Skip to content

Instantly share code, notes, and snippets.

@rday
Created June 13, 2013 23:41
Show Gist options
  • Save rday/5778365 to your computer and use it in GitHub Desktop.
Save rday/5778365 to your computer and use it in GitHub Desktop.
A brief example
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