为什么libcpp.vector这么慢?

时间:2012-09-09 02:15:07

标签: cython

我正在使用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的四行时,我们得到了这个:

  • Cython:2.80126094818秒
  • Python:2.13021802902秒

我是否遗漏了某些东西,或者字符串的矢量非常慢?

0 个答案:

没有答案