Vector 在包裹C++向量时移除Python开销

Vector 在包裹C++向量时移除Python开销,vector,cython,Vector,Cython,我已经尝试过包装向量,以便在Python dict中使用它们 这似乎会产生大量的开销。例如,参见第72行和第75行。他们只需将一个整数添加到向量中已有的数字: 是否有可能消除这种开销,或者这是我为包装向量所付出的代价?这似乎是基于。将uuu getitem和uuu setitem添加到cdef类向量的目的纯粹是为了从Python对其进行索引。从Cython可以直接索引到C++向量中,以获得额外的速度。 在文件的开头添加以下行: from libcpp.algorithm cimport sort

我已经尝试过包装向量,以便在Python dict中使用它们

这似乎会产生大量的开销。例如,参见第72行和第75行。他们只需将一个整数添加到向量中已有的数字:

是否有可能消除这种开销,或者这是我为包装向量所付出的代价?

这似乎是基于。将uuu getitem和uuu setitem添加到cdef类向量的目的纯粹是为了从Python对其进行索引。从Cython可以直接索引到C++向量中,以获得额外的速度。 在文件的开头添加以下行:

from libcpp.algorithm cimport sort as stdsort
from libcpp.algorithm cimport unique
from libcpp.vector cimport vector
# from libcpp cimport bool
cimport cython

@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
cdef class Vector:
    cdef vector[cython.int] wrapped_vector

    # the easiest thing to do is add short wrappers for the methods you need
    def push_back(self, int num):
        self.wrapped_vector.push_back(num)

    def sort(self):
        stdsort(self.wrapped_vector.begin(), self.wrapped_vector.end())

    def unique(self):
        self.wrapped_vector.erase(unique(self.wrapped_vector.begin(), self.wrapped_vector.end()), self.wrapped_vector.end())


    def __str__(self):
        return "[" + ", ".join([str(i) for i in self.wrapped_vector]) + "]"

    def __repr__(self):
        return str(self)

    def __len__(self):
        return self.wrapped_vector.size()

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.initializedcheck(False)
    def __setitem__(self, int key, int item):
        self.wrapped_vector[key] = item

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.initializedcheck(False)
    def __getitem__(self, int key):
        return self.wrapped_vector[key]
cdef Vector v
这将使Cython确保分配给v的任何对象都是向量对象,否则将引发TypeError,从而允许您直接访问其cdef属性

然后更改行:

from libcpp.algorithm cimport sort as stdsort
from libcpp.algorithm cimport unique
from libcpp.vector cimport vector
# from libcpp cimport bool
cimport cython

@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
cdef class Vector:
    cdef vector[cython.int] wrapped_vector

    # the easiest thing to do is add short wrappers for the methods you need
    def push_back(self, int num):
        self.wrapped_vector.push_back(num)

    def sort(self):
        stdsort(self.wrapped_vector.begin(), self.wrapped_vector.end())

    def unique(self):
        self.wrapped_vector.erase(unique(self.wrapped_vector.begin(), self.wrapped_vector.end()), self.wrapped_vector.end())


    def __str__(self):
        return "[" + ", ".join([str(i) for i in self.wrapped_vector]) + "]"

    def __repr__(self):
        return str(self)

    def __len__(self):
        return self.wrapped_vector.size()

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.initializedcheck(False)
    def __setitem__(self, int key, int item):
        self.wrapped_vector[key] = item

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.initializedcheck(False)
    def __getitem__(self, int key):
        return self.wrapped_vector[key]
cdef Vector v
致:

对于其他索引行也是如此


注意,BuffSeCKEFALSE和WrAbActuple Falm对于C++对象来说完全没有作用。C++索引操作符不执行边界检查,Cython不添加它,也不支持否定索引。boundscheck和wraparound仅适用于索引MemoryView或numpy数组。

您正在创建一个Python对象,一个字典。但顺便说一句,这看起来只是为最后一个文件生成了标记,丢弃了之前读取的结果。是的,代码的逻辑还不重要,但有一个好的提示:我在哪里创建Python dict?将_reads _添加到_dict做什么?我看到了tags.values和tags.items以及Python字典方法的几种用法。对不起!重要信息。将读取添加到dict创建向量dict。所以上面的v是向量: