Python 字典与 C++ 标准 :unordered_map (cython) vs cythonized python dict

标签 python c++ dictionary cython unordered-map

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

  • 使用 std::unordered_map 和 Cython book notation 的 Cython CPP 代码(定义一对并使用插入方法)
  • 使用 std::unordered_map 和 python 符号 (map[key] = value) 的 Cython CPP 代码
  • 使用 python 字典 (map[key] = value) 的 Cython 代码(类型化代码)
  • 纯 python 代码

我期待看到 cython 代码如何胜过纯 python 代码,但在这种情况下没有改进。这可能是什么原因?我正在使用 Cython-0.22、python-3.4 和 g++-4.8。

我使用 timeit 获得了这个执行时间(秒):

  • Cython CPP book notation -> 15.696417249999968
  • Cython CPP python 符号 -> 16.481350984999835
  • Cython python 符号 -> 18.585355018999962
  • 纯 python -> 18.162724677999904

代码在这里,你可以使用它:

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

map 示例.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)

use_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。

关于Python 字典与 C++ 标准 :unordered_map (cython) vs cythonized python dict,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29268914/

相关文章:

c++ - 如何从 C++ 中触发 QML 动画

c++ - 是否有 .at() 等效于 multimap ?

c++ - 鉴于这些条件,我可以使用 STL 进行线程化吗?

Python:从excel中的数据创建字典

python - 如何在较小的范围内绘图

Python Cmd/Powershell 错误 "Traceback (Most recent call last)"

python - 神经网络不学习(损失保持不变)

python - 在 QListView 中使用 QStyledItemDelegates 作为自定义项

c++ - 优化switch(x),x是作为参数传递的常量

c++ - 使用 SYSTEMTIME、FILETIME 和 ULARGE_INTEGER 修改日期和时间值