Skip to content

Instantly share code, notes, and snippets.

@photizzo
Created January 6, 2018 00:17
Show Gist options
  • Save photizzo/2a1e889ba7566bbe25109f0ade88ca06 to your computer and use it in GitHub Desktop.
Save photizzo/2a1e889ba7566bbe25109f0ade88ca06 to your computer and use it in GitHub Desktop.
import numpy as np
import time
a = (np.random.rand(1,1000000)) #create a random (1,10) array
result1 = np.zeros((1,1000000),dtype=np.int) #create an array of zeros to hold the result
#print ("a: \n" + str(a))
tic = time.time()
for i in range(1000000):
if a[0,i]>0.5:
result1[0,i] = 1
else:
result1[0,i] = 0
#print ("result: \n" +str(result))
toc = time.time()
print ("time passed for result1: " + str(toc-tic) + "ms")
tic = time.time()
result2 = (np.where(a>0.5,1,0))
#print ("result2: \n" +str(result2))
toc = time.time()
print ("time passed for result2: " + str(toc-tic) + "ms")
assert((result1 == result2).all)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment