- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
通常,当将 Python 和 C 代码粘合在一起时,需要将 Python 列表转换为连续的内存,例如一个 array.array
。这个转换步骤成为瓶颈也很常见,所以我发现自己用 Cython 做一些愚蠢的事情,因为它比内置的 Python 解决方案更快。
例如,将 Python 列表 lst
转换为 int32
连续内存我知道两种可能性:
a=array.array('i', lst)
和
a=array.array('i');
a.fromlist(lst)
但是它们都比下面的 cython 版本慢:
%%cython
import array
from cpython cimport array
def array_from_list_iter(lst):
cdef Py_ssize_t n=len(lst)
cdef array.array res=array.array('i')
cdef int cnt=0
array.resize(res, n) #preallocate memory
for i in lst:
res.data.as_ints[cnt]=i
cnt+=1
return res
我的时间显示(Linux、Python3.6,但 Windows 和/或 Python2.7 的结果非常相似),cython 解决方案快了大约 6 倍:
Size new_array from_list cython_iter factor
1 284ns 347ns 176ns 1.6
10 599ns 621ns 209ns 2.9
10**2 3.7µs 3.5µs 578ns 6.1
10**3 38.5µs 32µs 4.3µs 7.4
10**4 343µs 316µs 40.4µs 7.8
10**5 3.5ms 3.4ms 481µs 7.1
10**6 34.1ms 31.5ms 5.0ms 6.3
10**7 353ms 316ms 53.3ms 5.9
以我对 CPython 的有限理解,我会说 from_list
-解决方案使用这个 build-in function :
static PyObject *
array_array_fromlist(arrayobject *self, PyObject *list)
{
Py_ssize_t n;
if (!PyList_Check(list)) {
PyErr_SetString(PyExc_TypeError, "arg must be list");
return NULL;
}
n = PyList_Size(list);
if (n > 0) {
Py_ssize_t i, old_size;
old_size = Py_SIZE(self);
if (array_resize(self, old_size + n) == -1)
return NULL;
for (i = 0; i < n; i++) {
PyObject *v = PyList_GetItem(list, i);
if ((*self->ob_descr->setitem)(self,
Py_SIZE(self) - n + i, v) != 0) {
array_resize(self, old_size);
return NULL;
}
}
}
Py_RETURN_NONE;
}
a=array.array('i', lst)
grows dynamically并且需要重新分配,所以这可以解释一些减速(但正如测量显示的那样,并没有太多!),但是 array_fromlist
预分配了所需的内存 - 它基本上与 Cython 的算法完全相同-代码。
那么问题来了:为什么这个 Python 代码比 Cython 代码慢 6 倍?我错过了什么?
下面是测量时间的代码:
import array
import numpy as np
for n in [1, 10,10**2, 10**3, 10**4, 10**5, 10**6, 10**7]:
print ("N=",n)
lst=list(range(n))
print("python:")
%timeit array.array('i', lst)
print("python, from list:")
%timeit a=array.array('i'); a.fromlist(lst)
print("numpy:")
%timeit np.array(lst, dtype=np.int32)
print("cython_iter:")
%timeit array_from_list_iter(lst)
numpy 解决方案比 python 版本慢约 2 倍。
最佳答案
最大的区别似乎是实际的 int 拆箱。 CPython 数组实现使用 PyArg_Parse而 cython 正在调用 PyLong_AsLong - 至少我认为,通过几层宏。
%%cython -a
from cpython cimport PyArg_Parse
def arg_parse(obj):
cdef int i
for _ in range(100000):
PyArg_Parse(obj, "i;array item must be integer", &i)
return i
def cython_parse(obj):
cdef int i
for _ in range(100000):
i = obj
return i
%timeit arg_parse(1)
# 2.52 ms ± 67.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%timeit cython_parse(1)
# 299 µs ± 1.86 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
关于python - 为什么内置 array.fromlist() 比 cython 代码慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48784935/
我试图在 (C) Python 源代码中找到内置 in 运算符的实现。我在内置函数源代码中搜索过,bltinmodule.c ,但找不到此运算符的实现。我在哪里可以找到这个实现? 我的目标是通过扩展此
我们正在开发一个 shell(学校项目)。我们不理解一种行为。为什么内置函数在重定向时不起作用? 喜欢 cd - | command 不改变目录。 或 export NAME=VALUE | comm
有人问有关如何对列表进行排序的问题。从基本List.Sort()到List.OrderBy()有几种方法。最可笑的是自己动手的SelectionSort。我迅速将其否决,但这使我思考。应用于列表的
我正在尝试使用 C 中内置的 qsort 函数对结构进行排序 typedef struct abc{ long long int fir; long long int sec; }abc; 在
我觉得有一些内置的东西。如果对象为空,我想要默认值(或者特别是 0,我只使用十进制/整数)。是否有编写此函数的内置方法? static int GetDecimalFromObject(object
Java 是否有用于生成和解析文档的内置 XML 库?如果不是,我应该使用哪个第三方? 最佳答案 Sun Java 运行时附带 Xerces 和 Xalan 实现,它们提供解析 XML(通过 DOM
我对 python 的“all”和生成器有以下问题: G = (a for a in [0,1]) all(list(G)) # returns False - as I expected 但是:
我有一些使用 gcc 内部函数的代码。我想包含代码以防缺少内在函数。我该怎么做? #ifdef __builtin_ctzll 不起作用。 最佳答案 使用最新版本的 clang,现在可以使用 __ha
人们常说应该在本地重新声明(某些)Lua 函数,因为这样可以减少开销。但这背后的确切规则/原则是什么?我怎么知道哪些功能应该完成,哪些是多余的?还是应该为每个功能完成,甚至是您自己的功能? 不幸的是,
我想实现以下功能: TestClass values 接受任意数量的 NewClass 对象 只有 NewClass 对象没有完全相同的属性值被添加到TestClass.values 我想出了这个:
我正在尝试编写一个存储过程(使用 SQL Server Management Studio 2008 R2)以从表中检索最大测量值。这似乎是一件容易的事,所以我写了一个简短的存储过程来获取 MAX。但
我刚写了我的第一个Electron应用程序。现在,我正在尝试通过electron-packager构建它。我的package.json看起来像这样: { "name": "pixelcast",
我正在寻找在 WPF 应用程序中使用的“安全”字体系列列表 - 应该安装在所有能够运行 WPF 的客户端机器上的字体系列。 Silverlight 有一个明确定义的列表( listed on MSDN
好吧,(在写了几次之后)发现System.Windows.Controls命名空间中已经有一个BooleanToVisibilityConverter,这真是一个惊喜。 可能还有更多这样隐藏的节省时间
在我的 gradle 构建文件中,我有以下插件 block plugins { `java-library` jacoco checkstyle } 这些都没有指定版本,但一切
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 3 年前。 Improve this ques
10 implementations String#reverse 已根据每个浏览器进行分析。 自 2011 年以来已对这些实现进行了解释。 当 ES6 出现时,有很多代码变得更加优雅和性能。 关于
在 Julia 包 BenchmarkTools 中,有一些像 @btime、@belapse 这样的宏对我来说似乎是多余的,因为 Julia 内置了@time、@elapse 宏。在我看来,这些宏服
我正在尝试编写一个简单的 LLVM 通行证,其目标如下: 查找所有 call指示。 在被调用函数中插入我编写的外部函数。 例如,考虑我有以下示例程序: #include #include int
我理解 'a) -> (rhs:'a -> 'a) -> 'a 在我感兴趣的情况下,我经常发现自己想要类似 (lhs:'a -> 'b) -> (rhs:'c -> 'b) -> 'b 的东西在侧面
我是一名优秀的程序员,十分优秀!