[SciPy-user] More on speed comparisons
Gael Varoquaux
gael.varoquaux@normalesup....
Mon Jun 16 09:59:16 CDT 2008
On Mon, Jun 16, 2008 at 10:45:10AM -0400, Ivo Maljevic wrote:
> I knew I should have refrained from sending my "findings". Especially
> because I was quick to
> jump to conclusions :(
> It turns out SciPy is as fast as octave when written in a vector form. The
> correct version of
> the array form of the script is (it was not using proper version of the
> sum function):
Indeed. I am also curious to know how you measure timings. The proper way
of mesuring timings (ie measuring CPU time, and not wall time) is using
the timeit module. You can either use the timeit shell command, or the
timeit magic, in ipython. Here are the results I get:
##########################################################################
# rand_test_1.py
from random import random
from math import sqrt, sin
def do_python():
N = 1000000
mean = 0
var = 0
for i in range(N):
x = random()
x = 3.14*sqrt(x)
x = sin(x)
mean += x
var += x**2
mean = mean/N
var = var/N - mean**2
print 'Mean=%g, var=%g' % (mean, var)
# rand_test_2.py
def do_numpy():
import numpy as np
N = 1000000
x = np.random.rand(N)
x = 3.14*np.sqrt(x)
x = np.sin(x)
mean = x.mean()
var = np.sum(x**2)/N - mean**2
print 'Mean=%g, var=%g' % (mean, var)
##########################################################################
And in ipython:
In [1]: %run test.py
In [2]: %timeit do_python()
[... snip ]
10 loops, best of 3: 1.18 s per loop
In [3]: %timeit do_numpy()
10 loops, best of 3: 147 ms per loop
This is significantly different from your timings. The numbers do not
mean the same thing, but I trust these one more, and would try to use CPU
time only for comparisons between different approachs.
My 2 cents.
Gaël
More information about the SciPy-user
mailing list