0
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?

24April14
  • 157
  • 1
  • 8
  • 1
    First one is Object type array and as such would be slow. Go through the linked dup for more info. – Divakar Jan 10 '17 at 13:04
  • It's easy to overlook this little line in the `frompyfunc` docs: `The returned ufunc always returns PyObject arrays`. You need something like `C = convert(Y).astype(int)` to get something like `Y`. (`C==Y` just tests the equality of the elements, not 'sameness' of the arrays). – hpaulj Jan 10 '17 at 17:19
  • Thank you for the comments. Now I understand the differences – 24April14 Jan 12 '17 at 11:40

0 Answers0