本文介绍了Python字典vs C ++ std:unordered_map(cython)vs cythonized python dict的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was trying to measure the performance between python dictionaries, cythonized python dictionaries and cythonized cpp std::unordered_map doing only a init procedure. If the cythonized cpp code is compiled I thought it should be faster than the pure python version. I did a test using 4 different scenario/notation options:

  • 使用std :: unordered_map和 Cython图书符号的Cython CPP代码(定义一对并使用插入方法)
  • 使用std :: unordered_map和python表示法(map [key] =值)的Cython CPP代码
  • 使用python字典(map [key] =值)的Cython代码(键入的代码)
  • 纯python代码
  • Cython CPP code using std::unordered_map and Cython book notation (defining a pair and using insert method)
  • Cython CPP code using std::unordered_map and python notation (map[key] = value)
  • Cython code (typed code) using python dictionaries (map[key] = value)
  • Pure python code

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

I was expecting see how cython code outperforms pure python code, but in this case there is not improvement. Which could be the reason? I'm using Cython-0.22, python-3.4 and g++-4.8.

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

I got this exec time (seconds) using timeit:

  • Cython CPP的书写方式-> 15.696417249999968
  • Cython CPP python表示法-> 16.481350984999835
  • Cython python表示法-> 18.585355018999962
  • 纯python-> 18.162724677999904

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

Code is here and you can use it:

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

map_example.pyx

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

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

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缩进:)之后)

My timings from your code (after correcting that python *10 indent :) ) are

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

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

Basically everyone is in the same ballpark, with a modest edge for the CPP versions.

我的机器没什么特别的,通常的Ubuntu 14.10、0.202 Cython,3.42 Python.

Nothing special about my machine, the usual Ubuntu 14.10, 0.202 Cython, 3.42 Python.

这篇关于Python字典vs C ++ std:unordered_map(cython)vs cythonized python dict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 08:07
查看更多