Cython を使用して Django モジュールの一部を高速化する必要があるかどうかを判断するために、いくつかの実験を行っています。私は多くの文字列と文字列のリストを使用しているので、これを試しました:
from libcpp.string cimport string
from libcpp.vector cimport vector
def cython_test_string():
cdef unsigned short int i = 0
cdef string s
# cdef vector[string] ls
for i in range(10000):
s.append('a')
# ls.push_back(s)
def python_test_string():
s = ''
# ls = []
for i in range(10000):
s += 'a'
# ls.append(s)
もちろん、C++ 文字列は Python オブジェクトよりも高速であるため、次の結果が得られます (100 回の反復の場合):
- Cython : 0.0110609531403 秒
- Python: 0.193608045578 秒
しかし、vector と list を扱う 4 行のコメントを外すと、次のようになります。
- Cython : 2.80126094818 秒
- Python: 2.13021802902 秒
何か不足していますか、それとも文字列のベクトルが非常に遅いですか?