- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
假设我有一个数组
from array import array
myarr = array('l', [1, 2, 3])
和一个变量:我的变量 = 4
创建新数组的最快方法是什么:
newarray = array('l', [1, 2, 3, 4])
您可以假设所有元素都是“长”类型
我尝试创建一个新数组并使用 array.append()
不确定它是否最快。我正在考虑像这样使用 memoryview
:malloc(4*sizeof(long))
但我不知道如何将较短的数组复制到内存 View 的一部分。然后将最后一个元素插入最后一个位置。
我是 Cython 的新手。感谢您的帮助!
更新:我比较了以下三种方法:
Cython: [100000 次循环,最好的 3 次:每次循环 5.94 微秒]
from libc.stdlib cimport malloc
def cappend(long[:] arr, long var, size_t N):
cdef long[:] result = <long[:(N+1)]>malloc((N+1)*sizeof(long))
result.base[:N] = arr
result.base[N] = var
return result
数组: [1000000 次循环,最好的 3 次:每次循环 1.21 微秒]
from array import array
import copy
def pyappend(arr, x):
result = copy.copy(arr)
result.append(x)
return result
列表追加: [1000000 次循环,3 次中的最佳次数:每次循环 480 纳秒]
def pylistappend(lst, x):
result = lst[:]
result.append(x)
return result
有希望改进cython部分,打败array one吗?
最佳答案
与“普通”python 相比,Cython 使我们能够更多地访问 array.array
的内部结构,因此我们可以利用它来加速代码:
7
因子(通过消除大部分开销)。2
进行。继续阅读以了解更多详情。
尝试为如此小的输入优化函数有点不寻常,但并非没有(至少理论上)兴趣。
那么让我们从您的函数作为基线开始:
a=array('l', [1,2,3])
%timeit pyappend(a, 8)
1.03 µs ± 10.4 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
lst=[1,2,3]
%timeit pylistappend(lst, 8)
279 ns ± 6.03 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
我们必须意识到:我们衡量的不是复制的成本,而是开销的成本(python解释器,调用函数等),例如a
有没有区别3 或 5 个元素:
a=array('l', range(5))
%timeit pyappend(a, 8)
1.03 µs ± 6.76 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
在数组版本中,我们有更多的开销,因为我们通过 copy
模块进行了间接访问,我们可以尝试消除它:
def pyappend2(arr, x):
result = array('l',arr)
result.append(x)
return result
%timeit pyappend2(a, 8)
496 ns ± 5.04 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
那更快。现在让我们使用 cython - 这将消除解释器成本:
%%cython
def cylistappend(lst, x):
result = lst[:]
result.append(x)
return result
%%cython
from cpython cimport array
def cyappend(array.array arr, long long int x):
cdef array.array res = array.array('l', arr)
res.append(x)
return res
%timeit cylistappend(lst, 8)
193 ns ± 12.4 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
%%timeit cyappend(a, 8)
421 ns ± 8.08 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
cython 版本对于 list
大约快 33%,对于 array
大约快 10%。构造函数 array.array()
需要一个可迭代对象,但我们已经有了一个 array.array
,所以我们使用 cpython
的功能来获取访问 array.array
对象的内部并稍微改善情况:
%%cython
from cpython cimport array
def cyappend2(array.array arr, long long int x):
cdef array.array res = array.copy(arr)
res.append(x)
return res
%timeit cyappend2(a, 8)
305 ns ± 7.25 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
下一步我们需要知道array.array
是如何追加元素的:通常,it over-allocates ,因此 append()
的摊销成本为 O(1)
,但是在 array.copy
之后,新数组正好是所需数量的元素,并且下一个 append
调用重新分配。我们需要更改它(有关所用函数的说明,请参阅 here):
%%cython
from cpython cimport array
from libc.string cimport memcpy
def cyappend3(array.array arr, long long int x):
cdef Py_ssize_t n=len(arr)
cdef array.array res = array.clone(arr,n+1,False)
memcpy(res.data.as_voidptr, arr.data.as_voidptr, 8*n)#that is pretty sloppy..
res.data.as_longlongs[n]=x
return res
%timeit cyappend3(a, 8)
154 ns ± 1.34 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
与您的函数类似,内存分配过度,因此我们不再需要调用 resize()
。现在我们比 list
更快,几乎比原始 python 版本快 7 倍。
让我们比较更大数组大小的时间 (a=array('l',range(1000))
, lst=list(range(1000))
,其中复制数据占用了大部分运行时间:
pyappend 1.84 µs #copy-module is slow!
pyappend2 1.02 µs
cyappend 0.94 µs #cython no big help - we are copying twice
cyappend2 0.90 µs #still copying twice
cyappend3 0.43 µs #copying only once -> twice as fast!
pylistappend 4.09 µs # needs to increment refs of integers
cylistappend 3.85 µs # the same as above
现在,消除 array.array
的不必要的副本给了我们预期的因子 2。
对于更大的数组(10000
元素),我们看到以下内容:
pyappend 6.9 µs #copy-module is slow!
pyappend2 4.8 µs
cyappend2 4.4 µs
cyappend3 4.4 µs
版本之间不再有区别(如果丢弃慢速复制模块)。原因是 array.array
对如此大量的元素改变了行为:当复制它时过度分配从而避免在第一个 append()
之后重新分配.
我们可以很容易地检查它:
b=array('l', array('l', range(10**3)))#emulate our functions
b.buffer_info()
[] (94481422849232, 1000)
b.append(1)
b.buffer_info()
[] (94481422860352, 1001) # another pointer address -> reallocated
...
b=array('l', array('l', range(10**4)))
b.buffer_info()
[](94481426290064, 10000)
b.append(33)
b.buffer_info()
[](94481426290064, 10001) # the same pointer address -> no reallocation!
关于python - Cython 从现有数组和变量创建新数组的最快方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46980273/
我有一个如下图所示的情节。对于这个情节,我想在情节(右下角或左下角)的某处添加类似的线图。我正在使用的子图的命令是 plot( 1:121, sample(1:121),type='l' ) 它绘制在
我有一个单表数据库,我继承并迁移到 SQL Server,然后通过创建、链接和填充一大堆表示主表中项目的查找类型表来规范化它。我现在想用它们的外键替换原始表中的那些项目。我是不是一直在写一堆查询或 U
我有一个 Web 应用程序,它当前正在从服务器获取 PDF 的 base64 表示。我可以使用 Mozilla 的 pdf.js 在 上显示它并使用下拉菜单切换页面。 根据我所能找到的一切和Can
在 DB2 上运行的 Moodle 2 安装中,删除用户不成功,返回从数据库读取错误: Debug info: [IBM][CLI Driver][DB2/LINUXX8664] SQL0206N "
我在grails项目的RH包中添加了一个名为Authorization的新域类。 然后,我从grails菜单自动生成了 Controller 和 View 。 但是当我尝试输入 Controller
今天,我发现了一个有趣的plunker,经过谷歌大量搜索后一无所获,希望我能在这里找到答案。我只是想要那个笨蛋的副本。我不想使用复制和粘贴技术。有什么方法可以获取已建立的 plunk 的副本吗?我如何
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: Migrate normal sqlite3 database to core data? 是否可以将现有的 sql
我正在尝试在我的应用程序上添加启动画面。我干净地构建了程序,但我选择了错误的文件。现在我第二次编辑了 VM 选项并再次干净构建,现在我收到此错误: C:\Users\User\Documents\Ne
我已经查看了很多问题,我不相信这是重复使用单元格的结果,因为新的单元格图像是正确的,但是现有的单元格图像不正确并且曾经是正确的。我会先发布图片,以便更容易理解问题。 我有一个图像单元的 Collect
我在来自 Vaadin 的 ContainerHierarchicalWrapper 的这段代码中有一个非常奇怪的错误: for (Object object : children.keySet())
到目前为止,我正在使用 Globalize用于我的 JavaScript 应用程序的 i18n 和 l10n(使用 jQuery UI 构建)。这行得通,但它将我的代码与另一个特定的库联系在一起。现在
我正在创建一个 JHipster 应用程序,现在确定了 full text search 的必要性.我知道 JHipster 与 Elasticseach 集成,但我在创建项目时没有启用它。有没有一种
我一直在寻找堆栈中的建议,但我仍然不能 100% 确定改进它的最佳方法。我有一个存储大约 130K 条记录的 mysql INNODB“产品”表。杂项产品数据等大约有 80 个字段,然后我们一直在为每
我在一本书上看到,它说:当我们使用另一个初始化新创建的对象时 - 使用复制构造函数创建一个临时对象,然后使用赋值运算符将值复制到新对象! 后来在书中我读到:当使用另一个对象初始化新对象时,编译器创建一
我第一次安装现有的 Django 项目时遇到了启动服务器 python manage.py runserver 的问题 这是我做的 1.克隆仓库, 2.制作虚拟环境 3.pip安装要求.txt 4.生
我有一个网站,还有一个登录表单。我不想使用 PHP 来检查我的 MySQL 数据库,因此我正在寻找一种方法来检查用户凭据以查看是否已有 Linux 用户。我知道 PAM,但我还没有找到任何有关如何从网
我有一个现有的 Umbraco 项目在 IIS 服务器上运行。当我开始这个项目时,我基本上是将 Umbraco 直接安装到服务器上,并通过管理界面进行编码,直到网站启动并上线。 现在,客户想要一些更改
我是 Android 开发新手,目前正在学习一些教程。当我在 Eclipse 中设置一个新的 Android 项目,并选择 Windows -> Android SDK and AVD Manager
我有这个注册页面可以正常工作,但对于电子邮件字段,我需要确保电子邮件正确有效1:正确2 : 有效 为了正确添加电子邮件,我正在使用 Java 脚本验证来维护abc@def.com 很好用 但我的问题是
首先让我说我不熟悉 COM 引用,并且我在 Windows 7 64 位计算机上使用 VS2010。今天早上,我从 TFS 中删除了一个现有项目。然后我尝试构建项目并收到此错误: The type o
我是一名优秀的程序员,十分优秀!