惯用的方式做列表/字典在用Cython? [英] Idiomatic way to do list/dict in Cython?

查看:248
本文介绍了惯用的方式做列表/字典在用Cython?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题:我发现,处理大型数据集的原始C ++使用STL的地图和矢量往往可以大大高于使用用Cython更快(低内存占用)。

My problem: I've found that processing large data sets with raw C++ using the STL map and vector can often be considerably faster (and with lower memory footprint) than using Cython.

我估计这个速度点球的那部分是由于使用Python列表和类型的字典,并可能有一些技巧地用Cython使用较少的担保数据结构。例如,此页面( http://wiki.cython.org/tutorials/numpy )展示了如何numpy的阵列在用Cython由predefining的ND数组的大小和类型非常快。

I figure that part of this speed penalty is due to using Python lists and dicts, and that there might be some tricks to use less encumbered data structures in Cython. For example, this page (http://wiki.cython.org/tutorials/numpy) shows how to make numpy arrays very fast in Cython by predefining the size and types of the ND array.

问题:有没有办法做到与清单/类型的字典,例如类似通过声明大致有多少元素或(键,值)对你希望在他们? 也就是说,有没有列出/ http://stardict.sourceforge.net/Dictionaries.php下载到(快速)的数据结构转换成用Cython一种惯用的方法?

Question: Is there any way to do something similar with lists/dicts, e.g. by stating roughly how many elements or (key,value) pairs you expect to have in them? That is, is there an idiomatic way to convert lists/dicts to (fast) data structures in Cython?

如果不是我想我就必须把它写在C ++中,并在用Cython进口包裹。

If not I guess I'll just have to write it in C++ and wrap in a Cython import.

推荐答案

用Cython现在有模板支撑,并附带声明对一些STL容器。

Cython now has template support, and comes with declarations for some of the STL containers.

请参阅http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html#standard-library

下面是他们给出的例子:

Here's the example they give:

from libcpp.vector cimport vector

cdef vector[int] vect
cdef int i
for i in range(10):
    vect.push_back(i)
for i in range(10):
    print vect[i]

这篇关于惯用的方式做列表/字典在用Cython?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆