Recently I answered to THIS question which wanted the multiplication of 2 lists,some user suggested the following way using numpy, alongside mine which I think is the proper way :
(a.T*b).T
Also I found that aray.resize()
has a same performance like that. any way another answer suggested a solution using list comprehension :
[[m*n for n in second] for m, second in zip(b,a)]
But after the benchmark I saw that the list comprehension performs very faster than numpy :
from timeit import timeit
s1="""
a=[[2,3,5],[3,6,2],[1,3,2]]
b=[4,2,1]
[[m*n for n in second] for m, second in zip(b,a)]
"""
s2="""
a=np.array([[2,3,5],[3,6,2],[1,3,2]])
b=np.array([4,2,1])
(a.T*b).T
"""
print ' first: ' ,timeit(stmt=s1, number=1000000)
print 'second : ',timeit(stmt=s2, number=1000000,setup="import numpy as np")
result :
first: 1.49778485298
second : 7.43547797203
As you can see numpy is approximately 5 time faster. but most surprising thing was that its faster without using transpose, and for following code :
a=np.array([[2,3,5],[3,6,2],[1,3,2]])
b=np.array([[4],[2],[1]])
a*b
The list comprehension still was 5 time faster.So besides of this point that list comprehensions performs in C here we used 2 nested loop and a zip
function So what can be the reason? Is it because of operation *
in numpy?
Also note that there is no problem with timeit
here I putted the import
part in setup
.
I also tried it with larger arras, the difference gets lower but still doesn't make sense :
s1="""
a=[[2,3,5],[3,6,2],[1,3,2]]*10000
b=[4,2,1]*10000
[[m*n for n in second] for m, second in zip(b,a)]
"""
s2="""
a=np.array([[2,3,5],[3,6,2],[1,3,2]]*10000)
b=np.array([4,2,1]*10000)
(a.T*b).T
"""
print ' first: ' ,timeit(stmt=s1, number=1000)
print 'second : ',timeit(stmt=s2, number=1000,setup="import numpy as np")
result :
first: 10.7480301857
second : 13.1278889179
See Question&Answers more detail:
os