- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试加速这个 python 函数:
def twoFreq_orig(z, source_z, num, den, matrix, e):
Z1, Z2 = np.meshgrid(source_z, np.conj(z))
Z1 **= num
Z2 **= den - 1
M = (e ** ((num + den - 2) / 2.0)) * Z1 * Z2
return np.sum(matrix * M, 1)
其中z
和source_z
是np.ndarray
(1d,dtype=np.complex128
),num
和den
是np.ndarray
(2d,dtype=np.float64
),矩阵
是一个 np.ndarray
(2d,dtype=np.complex128
),e
是一个 np.float64
>.
我对 Numba 没有太多经验,但是在阅读了一些教程之后,我想出了这个实现:
@nb.jit(nb.f8[:](nb.c16[:], nb.c16[:], nb.f8[:, :], nb.f8[:, :], nb.c16[:, :], nb.f8))
def twoFreq(z, source_z, num, den, matrix, e):
N1, N2 = len(z), len(source_z)
out = np.zeros(N1)
for r in xrange(N1):
tmp = 0
for c in xrange(N2):
n, d = num[r, c], den[r, c] - 1
z1 = source_z[c] ** n
z2 = z[r] ** d
tmp += matrix[r, c] * e ** ((n + d - 1) / 2.0) * z1 * z2
out[r] = tmp
return out
不幸的是,Numba 实现不但没有加速,反而比原来慢了好几倍。我不知道如何正确使用 Numba。有哪位 Numba 专家可以帮助我吗?
最佳答案
实际上,如果不对数组的属性有更多的了解,我认为您无法做太多事情来加速 numba 函数(是否有一些数学技巧可以更快地完成一些计算)。
但我注意到一个错误:例如,您没有在 numba 版本中结合数组,并且我编辑了一些行以使其更加精简(其中一些可能只是口味)。我已经在适当的地方添加了评论:
@nb.njit
def twoFreq(z, source_z, num, den, matrix, e):
#Replace z with conjugate of z (otherwise the result is wrong!)
z = np.conj(z)
# Size instead of len() don't know if it actually makes a difference but it's cleaner
N1, N2 = z.size, source_z.size
# Must be zeros_like otherwise you create a float array where you want a complex one
out = np.zeros_like(z)
# I'm using python 3 so you need to replace this by xrange later
for r in range(N1):
for c in range(N2):
n, d = num[r, c], den[r, c] - 1
z1 = source_z[c] ** n
z2 = z[r] ** d
# Multiply with 0.5 instead of dividing by 2
# Work on the out array directly instead of a tmp variable
out[r] += matrix[r, c] * e ** ((n + d - 1) * 0.5) * z1 * z2
return out
def twoFreq_orig(z, source_z, num, den, matrix, e):
Z1, Z2 = np.meshgrid(source_z, np.conj(z))
Z1 **= num
Z2 **= den - 1
M = (e ** ((num + den - 2) / 2.0)) * Z1 * Z2
return np.sum(matrix * M, 1)
numb = 1000
z = np.random.uniform(0,1,numb) + 1j*np.random.uniform(0,1,numb)
source_z = np.random.uniform(0,10,numb) + 1j*np.random.uniform(0,1,numb)
num = np.random.uniform(0,1,(numb,numb))
den = np.random.uniform(0,1,(numb,numb))
matrix = np.random.uniform(0,1,(numb,numb)) + 1j*np.random.uniform(0,1,(numb, numb))
e = 5.5
# This failed for your initial version:
np.testing.assert_array_almost_equal(twoFreq(z, source_z, num, den, matrix, e),
twoFreq_orig(z, source_z, num, den, matrix, e))
我的计算机上的运行时间是:
%timeit twoFreq(z, source_z, num, den, matrix, e)
1 loop, best of 3: 246 ms per loop
%timeit twoFreq_orig(z, source_z, num, den, matrix, e)
1 loop, best of 3: 344 ms per loop
它比您的 numpy 解决方案快大约 30%。但我认为通过巧妙地使用广播,numpy 解决方案可以变得更快一些。但尽管如此,我获得的大部分加速都是来自省(introspection)略签名:请注意,您可能使用 C 连续数组,但您给出了任意顺序(因此 numba 可能会慢一点,具体取决于计算机体系结构)。可能通过定义 c16[::-1]
你会得到相同的速度,但通常只是让 numba 推断类型,它可能会尽可能快。异常(exception):您希望每个变量有不同的精度输入(例如您希望 z
为 complex128
和 complex64
)
当您的 numpy 解决方案内存不足时,您将获得惊人的加速(因为您的 numpy 解决方案已矢量化,因此需要更多 RAM!)使用 numb = 5000
numba 版本的速度大约快了 3 倍比 numpy 的要好。
通过巧妙的广播,我的意思是
np.conj(z[:,None]**(den-1)) * source_z[None, :]**(num)
等于
z1, z2 = np.meshgrid(source_z, np.conj(z))
z1**(num) * z2**(den-1)
但在第一个变体中,您只能对 numb
元素进行幂运算,而您有一个 (numb, numb)
形状的数组,因此您可以执行更多“幂”操作不必要的操作(尽管我猜对于小数组,结果可能大部分被缓存并且不是很昂贵)
没有 mgrid
的 numpy 版本(产生相同的结果)如下所示:
def twoFreq_orig2(z, source_z, num, den, matrix, e):
z1z2 = source_z[None,:]**(num) * np.conj(z)[:, None]**(den-1)
M = (e ** ((num + den - 2) / 2.0)) * z1z2
return np.sum(matrix * M, 1)
关于python - 如何用 numba 加速这个 python 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35832877/
我想在我的 iPhone 应用程序中加入线性回归。经过一些搜索,我发现 Accelerate Framework 中的 LAPACK 和 BLAS 是正确的库。但是我很难将加速框架添加到我的 XCod
有什么方法可以加速 JS 脚本(我指的是一些复杂的 DOM 操作,比如游戏或动画)? 最佳答案 真的没有办法真正加快速度。您可以压缩它,但不会快很多。 关于Javascript 加速?,我们在Stac
有时,我必须为一个项目重新导入数据,从而将大约 360 万行读入 MySQL 表(目前是 InnoDB,但我实际上并不局限于这个引擎)。 “加载数据文件...”已被证明是最快的解决方案,但它有一个权衡
在尝试计算加速时,我被卡住了。所以给出的问题是: 问题 1 如果程序的 50% 增强了 2 倍,其余 50% 增强了 4 倍,那么由于增强而导致的整体加速是多少? Hints:考虑增强前(未增强)机器
目前我正在处理实时绘图,但可视化非常慢。我想知道你可以做些什么来加速 Matplotlib 中的事情: 后端如何影响性能?是否有后端 实时绘图比其他人更好吗? 我可以降低分辨率以提高 FPS 吗? 如
我有一个小型测试框架。它执行一个循环,执行以下操作: 生成一个小的 Haskell 源文件。 使用 runhaskell 执行此操作.该程序生成各种磁盘文件。 处理刚刚生成的磁盘文件。 这种情况发生了
这是我的网站:Instant-YouTube 如您所见,加载需要很长时间。在 IE8 及以下甚至有时会导致浏览器崩溃。我不确定是什么原因造成的。可能是 Clicksor 广告,但我认为是 swfobj
是否可以加速 SKSpriteNode? 我知道可以使用 node.physicsBody.velocity 轻松设置速度但是设置它的加速度有多难? 最佳答案 从牛顿第二定律倒推运动:F = m.a您
有没有人有加速 FCKEditor 的技术?是否有一些关键的 JavaScript 文件可以缩小或删除? 最佳答案 在最新版本 (3.0.1) 中,FCKEditor 已重命名为 CKEditor .
我有以下 MySQL 查询,需要一天多的时间才能执行: SELECT SN,NUMBER FROM a WHERE SN IN (SELECT LOWER_SN FROM b WHER
我现在正在开发一款使用加速来玩的游戏。我找到了如何让我的元素移动,但不改变它的“原点”,或者更准确地说,改变加速度计算的原点: 事实上,我的图像是移动的,它的中心是这样定义的: imageView.c
我有一个 mysql 表,其中存储有 4 列的成员消息: message_id(主键,自增) sender_id( key ) receiver_id( key ) 消息内容 我做了很多 SELECT
我在 cuda_computation.cu 中有以下代码 #include #include #include #include void checkCUDAError(const char
我正在使用 BeautifulSoup 在 for 循环中解析数千个网站。这是我的代码片段: def parse_decision(link): t1 = time.time() de
我正在使用 OpenCV 2.4 (C++) 在灰度图像上进行寻线。这涉及一些基本的图像处理步骤,如模糊、阈值、Canny 边缘检测器、梯度滤波器或霍夫变换。我必须在数千张图像上应用寻线算法。 考虑到
当我试图连续生成四次相同的报告时,我刚刚分析了我的报告应用程序。第一个用了 1859 毫秒,而后面的只用了 400 到 600 毫秒。对此的解释是什么?我能以某种方式使用它来使我的应用程序更快吗?报告
当我打开 Storyboard文件时,由于其中包含的 VC 数量,打开它需要 1-2 分钟。加快速度的最佳做法是什么?我们应该将一些 VC 移动到不同的 Storyboard文件中吗?我们是否应该使用
我有一个包含多个页面的 UIPageViewController。每个页面都是相同的 View Controller ,但会跟踪页码并显示 PDF 的正确页面。问题是每个 PDF 页面都需要在 cur
这实际上是两个问题,但它们非常相似,为了简单起见,我想将它们放在一起: 首先:给定一个已建立的 Java 项目,除了简单的代码内优化之外,还有哪些不错的方法可以加快它的速度? 其次:在用Java从头写
我有一个包含 1000 个条目的文档,其格式类似于:
我是一名优秀的程序员,十分优秀!