dot products

Alan G Isaac aisaac at american.edu
Mon Dec 20 22:53:40 EST 2004


"Alan G Isaac" <aisaac at american.edu> wrote in message
news:10sf50mr56l6f65 at corp.supernews.com...
> This times faster than the alternatives I have seen mentioned so far,
> given scipy.

Actually, since I am new to 'timeit', I probably should check that
I am not overlooking something.  Especially since I see an order
of magnitude difference in performance.  Does the code below
give the right comparisons?

Thanks,
Alan Isaac

#-------------------------------------------------------------
import timeit
env1='''
from operator import mul
from itertools import imap
def innerprod(x,y):
 return sum(imap(mul,x,y))
from scipy import rand
x=rand(50); y=rand(50)
'''
env2='''
from operator import mul
from itertools import imap
from scipy import rand
x=rand(50); y=rand(50)
'''
env3='''
from scipy import rand,dot
x=rand(50); y=rand(50)
'''
t1=timeit.Timer("innerprod(x,y)",env1)
t2=timeit.Timer("sum(imap(mul,x,y))",env2)
t3=timeit.Timer("dot(x,y)",env3)
trials=1000
print t1.repeat(2,trials)   #about 0.1 seconds
print t2.repeat(2,trials)   #about 0.1 seconds
print t3.repeat(2,trials)   #about 0.01 seconds






More information about the Python-list mailing list