如何使gmpy数组操作更快? [英] How do I make gmpy array operations faster?

查看:93
本文介绍了如何使gmpy数组操作更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用gmpy模块时,我在速度上遇到了麻烦.

I've been having trouble with speed while trying to utilise the gmpy module.

import numpy as np
import gmpy2 as gm
N = 1000
a = range(N)
%timeit [gm.sin(x) for x in a]
# 100 loops, best of 3: 7.39 ms per loop
%timeit np.sin(a)
# 10000 loops, best of 3: 198 us per loop

我想知道我是否可以以某种方式加快计算速度.我以为JIT或多处理程序可能会有所帮助,但我还没有弄清楚该怎么做.

I was wondering if I could somehow speed this computation. I was thinking JIT or multiprocessing might help but I haven't figured out how to do it.

任何帮助将不胜感激.如果您想让我发布更多信息,请告诉我.

Any help would be greatly appreciated. If you want me to post more information please let me know.

推荐答案

我很好奇如何提高性能,于是为gmpy2编写了一个新函数,该函数完全用C语言计算了列表的sin .不幸的是,并没有太大的进步.

I was curious to see how much performance increase would be possible so wrote a new function for gmpy2 that calculated the sin of a list entirely in C. Unfortunately, there wasn't much improvement.

%timeit [gmpy2.sin(x) for x in a]
100 loops, best of 3: 4.85 ms per loop
%timeit map(gmpy2.sin, a)
100 loops, best of 3: 4.59 ms per loop
%timeit gmpy2.vector(a)
100 loops, best of 3: 4.44 ms per loop

gmpy2不会释放全局解释器锁定(GIL),因此线程化将无济于事.

gmpy2 does not release the Global Interpreter Lock (GIL) so threading won't help.

多处理可能会有所帮助,但您可能需要并行执行需要几秒钟(或更长时间)的代码部分,以克服将数据传递给另一个进程的开销.

Multiprocessing may help but you will probably need to parallelize portions of code that take seconds (or longer) to execute to overcome the overhead of passing data to another process.

基于软件的任意精度浮点仅比本机浮点慢.

Software-based, arbitrary-precision floating point is just slower than native floating point.

这篇关于如何使gmpy数组操作更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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