Created
June 13, 2013 23:41
-
-
Save rday/5778365 to your computer and use it in GitHub Desktop.
A brief example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import numpy as np | |
def naive(): | |
x = 0 | |
y = 0 | |
for i in range(1, 1001): | |
x += pow(i, 2) | |
y += i | |
return pow(y, 2) - x | |
def excellence(): | |
x = np.arange(1001, dtype='int64') | |
sumsq = np.sum(x ** 2) | |
sqsum = x.sum() ** 2 | |
return sqsum - sumsq | |
start = time.time() | |
for i in range(4): | |
naive() | |
end = time.time() | |
print "Naive duration %.5f" % (end - start) | |
start = time.time() | |
for i in range(4): | |
excellence() | |
end = time.time() | |
print "Excellent duration %.5f" % (end - start) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment