[SciPy-user] compare scipy and matlab
Yosef Meller
yosefmel@post.tau.ac...
Mon Jun 8 07:13:21 CDT 2009
On Monday 08 June 2009 11:19:12 oyster wrote:
> what I want to know is the truth and availabilty to use scipy in
> serious works. Because I have met scipy bug but it has been fixed
> soon; and now I met 2(maybe 1) bugs with sympy, so I regard
> opensoftware(or any software?) with some suspicion.
>
> the following is written by lac. I hope he will not charge me for my post
> ;)
If he does, refuse to pay, for this code is over 8 times slower than it should
be! See below.
> ####### speed #########
>
> # python
>
> >>> def xperm(L):
>
> if len(L) <= 1:
> yield L
> else:
> for i in range(len(L)):
> for p in xperm(L[:i]+L[i+1:]):
> yield [L[i]] + p
>
> >>> def test(i):
>
> for p in xperm(map(lambda x:x+1, range(i))): pass;
>
> >>> import timeit
> >>> timeit.Timer('test(5)','from __main__ import test').timeit()
>
> 1263.9717730891925
from numpy import arange
from numpy.random import permutation
And then:
In [17]: %timeit test(5) # Your version
1000 loops, best of 3: 444 µs per loop
In [18]: %timeit test(5)
1000 loops, best of 3: 444 µs per loop
First get rid of the map (low hanging fruit):
In [19]: def test2(i):
for p in xperm(range(1, i+1)): pass
....:
In [21]: %timeit test2(5)
1000 loops, best of 3: 429 µs per loop
In [22]: %timeit test2(5)
1000 loops, best of 3: 430 µs per loop
In [23]: %timeit test2(5)
1000 loops, best of 3: 431 µs per loop
Now use numpy and sets:
In [38]: def xperm2(L):
tried = set()
while len(tried) < len(L):
new_perm = tuple(permutation(L))
if new_perm in tried: continue
tried.add(new_perm)
yield new_perm
In [45]: %timeit test2(5)
10000 loops, best of 3: 86.9 µs per loop
In [46]: %timeit test2(5)
10000 loops, best of 3: 86.2 µs per loop
And finally numpy.arange FTW:
In [47]: def test2(i):
for p in xperm2(arange(1, i+1)): pass
In [51]: %timeit test2(5)
10000 loops, best of 3: 51.9 µs per loop
In [52]: %timeit test2(5)
10000 loops, best of 3: 51.4 µs per loop
Anyone have a faster, vectorized way?
More information about the SciPy-user
mailing list