Python字典与C++std:unordered_map(cython)与cython化的Python字典

Python dictionaries vs C++ std:unordered_map (cython) vs cythonized python dict

本文关键字:cython 字典 Python 化的 unordered C++std map      更新时间:2023-10-16

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

  • Cython CPP代码使用std::unordered_map和Cython book表示法(定义一对并使用insert方法)
  • Cython CPP代码使用std::unordereded_map和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.6966417249999968
  • Cython CPP python表示法->16.481350984999835
  • Cython-python表示法->18.58535501899962
  • 纯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

setup_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))

我从您的代码中得到的时间(在更正python*10缩进后:)是

Cython CPP book notation
21.617647969018435
Cython CPP python notation
21.229907534987433
Cython python notation
24.44413448998239
Pure python
23.609809526009485

基本上每个人都在同一个球场上,CPP版本的优势不大。

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