Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save walkingmask/4f473335e9e319f4a9421ac750423103 to your computer and use it in GitHub Desktop.
Save walkingmask/4f473335e9e319f4a9421ac750423103 to your computer and use it in GitHub Desktop.
import numpy as np
import timeit
def f(x, y):
return (x+1)*y
def ff(X, y):
res = []
for x in X:
res.append(f(x,y))
return res
def mf(X, y):
return list(map(lambda x: f(x,y), X))
def cf(X, y):
return [f(x,y) for x in X]
vf = np.vectorize(f)
X = [i for i in range(10000)]
y = 12345
print(timeit.timeit('ff(X,y)', globals=globals(), number=1000))
print(timeit.timeit('mf(X,y)', globals=globals(), number=1000))
print(timeit.timeit('cf(X,y)', globals=globals(), number=1000))
print(timeit.timeit('vf(X,y)', globals=globals(), number=1000))
@walkingmask
Copy link
Author

results

$ ~/Desktop/compare_for_map_comprehension_vectorize.py
2.7477957619994413
2.872335820000444
2.09746183900279
3.430644760002906

The process was too simple?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment