Python字典与C++;std:unordered_映射(cython)与cythonizedpythondict

Python字典与C++;std:unordered_映射(cython)与cythonizedpythondict,python,c++,dictionary,cython,unordered-map,Python,C++,Dictionary,Cython,Unordered Map,我试图测量python字典、cythonized python字典和cythonized cpp std::unordered_map之间的性能,只执行一个init过程。如果编译cythonized cpp代码,我认为它应该比纯python版本更快。我使用4种不同的场景/符号选项进行了测试: Cython CPP代码使用std::无序_映射和(定义一对并使用insert方法) Cython CPP代码,使用std::无序_映射和python表示法(map[key]=value) 使用pytho

我试图测量python字典、cythonized python字典和cythonized cpp std::unordered_map之间的性能,只执行一个init过程。如果编译cythonized cpp代码,我认为它应该比纯python版本更快。我使用4种不同的场景/符号选项进行了测试:

  • Cython CPP代码使用std::无序_映射和(定义一对并使用insert方法)
  • Cython CPP代码,使用std::无序_映射和python表示法(map[key]=value)
  • 使用python字典的Cython代码(类型化代码)(map[key]=value)
  • 纯python代码
我希望看到cython代码的性能如何优于纯python代码,但在本例中没有改进。原因可能是什么?我正在使用Cython-0.22、python-3.4和g++-4.8

我使用timeit获得此执行时间(秒):

  • Cython CPP图书符号->15.69641724999968
  • Cython CPP python表示法->16.481350984999835
  • Cython python表示法->18.585355018999962
  • 纯python->18.16272467799904
代码在这里,您可以使用它:

cython -a map_example.pyx
python3 setup_map.py build_ext --inplace
python3 use_map_example.py
map_example.pyx

from libcpp.unordered_map cimport unordered_map
from libcpp.pair cimport pair

cpdef int example_cpp_book_notation(int limit):
    cdef unordered_map[int, int] mapa
    cdef pair[int, int] entry

    cdef int i

    for i in range(limit):
        entry.first = i
        entry.second = i
        mapa.insert(entry)
    return 0

cpdef int example_cpp_python_notation(int limit):
    cdef unordered_map[int, int] mapa
    cdef pair[int, int] entry

    cdef int i

    for i in range(limit):
        mapa[i] = i

    return 0


cpdef int example_ctyped_notation(int limit):
    mapa = {}
    cdef int i
    for i in range(limit):
        mapa[i] = i
    return 0
设置\u map.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
from Cython.Distutils import build_ext

import os

os.environ["CC"] = "g++"
os.environ["CXX"] = "g++"


modules = [Extension("map_example",
                 ["map_example.pyx"],
                 language = "c++",
                 extra_compile_args=["-std=c++11"],
                 extra_link_args=["-std=c++11"])]

setup(name="map_example",
     cmdclass={"build_ext": build_ext},
     ext_modules=modules)
使用_map_example.py

import map_example

C_MAXV = 100000000
C_NUMBER = 10

def cython_cpp_book_notation():
    x = 1
    while(x<C_MAXV):
        map_example.example_cpp_book_notation(x)
        x *= 10

def cython_cpp_python_notation():
    x = 1
    while(x<C_MAXV):
        map_example.example_cpp_python_notation(x)
        x *= 10

def cython_ctyped_notation():
    x = 1
    while(x<C_MAXV):
        map_example.example_ctyped_notation(x)
        x *= 10


def pure_python():
    x = 1
    while(x<C_MAXV):
        map_a = {}
        for i in range(x):
            map_a[i] = i
        x *= 10
    return 0


if __name__ == '__main__':
    import timeit

    print("Cython CPP book notation")
    print(timeit.timeit("cython_cpp_book_notation()", setup="from __main__ import cython_cpp_book_notation", number=C_NUMBER))


    print("Cython CPP python notation")
    print(timeit.timeit("cython_cpp_python_notation()", setup="from __main__ import cython_cpp_python_notation", number=C_NUMBER))


    print("Cython python notation")
    print(timeit.timeit("cython_ctyped_notation()", setup="from __main__ import cython_ctyped_notation", number=C_NUMBER))

    print("Pure python")
    print(timeit.timeit("pure_python()", setup="from __main__ import pure_python", number=C_NUMBER))
import map\u示例
C_最大值=100000000
C_数=10
def cython_cpp_book_notation():
x=1
而(x我的代码计时(在更正python*10缩进:)是

基本上每个人都在同一个球场上,对于CPP版本来说都有一定的优势


我的机器没有什么特别之处,通常的Ubuntu 14.10,0.202 Cython,3.42 Python。

你能在感兴趣的循环中放置一个计时器吗?我在纯Python循环中发现了一个问题,我修复并更新了时间和代码。无论如何,它还是比cython?快?。我会在有趣的循环中加上时间你是对的。我重新运行了代码,并在帖子中更新了exec时间。也许为了看到一些差异,操作地图值(找到一个特定的键,操作地图的值,等等)是必要的。所以,根本不用麻烦使用cpp的
unordered_map
?@avocado Python的dict已经高度优化了。没有理由。内存使用情况如何?
Cython CPP book notation
21.617647969018435
Cython CPP python notation
21.229907534987433
Cython python notation
24.44413448998239
Pure python
23.609809526009485