How do I make gmpy array operations faster?
Asked Answered
M

1

1

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

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.

Mannerless answered 28/10, 2014 at 3:33 Comment(0)
C
3

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 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.

Crackpot answered 28/10, 2014 at 5:23 Comment(3)
Thank you for your reply @casevh. I assume the gmpy2.vector(a) is the new function? Indeed it doesn't seem like a great speed up. With regards to multiprocessing I have been trying to use IPython's ipython.org/ipython-doc/stable/parallel/… framework. That should work right?Mannerless
Yes, vector() is the name of the test function. I was disappointed by the lack of speedup. I'm sorry, but I'm not familiar with IPython's parallel tools.Crackpot
No problem thank you very much. At least I know it's isn't worth my while to try to use some sort of JIT!Mannerless

© 2022 - 2024 — McMap. All rights reserved.