Python字典与C++std:unordered_map(cython)与cython化的Python字典
Python dictionaries vs C++ std:unordered_map (cython) vs cythonized python dict
我试图测量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。
相关文章:
- C++中的MSD基排序(字典序)
- C++中是否有任何函数等同于python中字典的get函数?
- 将 PCL 链接到 Cython C++ 模块
- 将数组/向量发送到 c++ 脚本的 cython 示例
- Cython:无法分配给 2D 矢量
- Cython通过浮点数的最快方式,用于高频控制回路
- 替代在python中制作邻接列表与图形问题的字典?(如 C++ 中的 vector<vector<int&g
- Cython 包含catkin_pkg设置的错误
- 在 Cython 中传递浮点数组的最佳选择
- 如何在 Cython 中定义返回 cpp 定义类型的函数?
- 通过Cython对Python的基本C++函数
- 在 Cython 中将C++向量转换为 numpy 数组,而无需复制
- 从python调用C++函数,使用cython,使用cythonize
- 如何通过 cython 将 numpy 数组列表传递给 C++
- 可以使用while(文件>>...C++成语在 Cython 中读取文件?
- "Cannot convert Python object argument to type '<typename>'" - 使用 Cython 包装 C++ 类时出错
- Cython中带有自定义比较器的优先级队列
- 重载的 operator() 在 Cython 中失败
- C++中类似Python字典的结构?
- Python字典与C++std:unordered_map(cython)与cython化的Python字典