I decided to try one of the first examples out on my own, it resulted in the following script:
import random
import numpy as np
from timeit import timeit
Z1 = random.sample(range(1000), 100)
Z2 = random.sample(range(1000), 100)
def add_python():
return [z1+z2 for (z1, z2) in zip(Z1, Z2)]
def add_numpy():
return np.add(Z1, Z2)
assert add_python() == add_numpy().tolist()
setup_str = "from __main__ import Z1, Z2, add_python, add_numpy"
print("add_python:", timeit("add_python()", setup_str))
print("add_numpy:", timeit("add_numpy()", setup_str))
The result is that in both python 3.5.2 and 2.7.13 python is in fact faster than numpy :)
No comments:
Post a Comment