import numpy as np
import time
X = np.random.randn(200, 200)
Y = np.random.randn(200, 200)
#version 1
def judge(x):
if x>0:
return 1
return x
convert = np.frompyfunc(judge,1,1)
C = convert(Y)
t1 = time.time()
r = X.dot(C)
t2 = time.time()
print t2-t1 #0.479473829269
#version 2
Y[Y>0] = 1
print C==Y #True
t1 = time.time()
r = X.dot(Y)
t2 = time.time()
print t2-t1 #0.00127196311951
Despite the two operands are the same, version 1 took 0.479s while version 2 took 0.001s and is 478 times faster than version 1. Why?