Skip to content

Instantly share code, notes, and snippets.

@CrazyPython
Created July 8, 2015 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CrazyPython/ad2a64393f71eb3a7087 to your computer and use it in GitHub Desktop.
Save CrazyPython/ad2a64393f71eb3a7087 to your computer and use it in GitHub Desktop.
Finished preparing.
Executing test 1
Executing test 2
Math module: maximmum 0.238082885742; minummum 0.166098117828; average 0.191208680471
Numpy module: maximmum 1.90435886383; minummum 1.6759428978; average 1.78847424189
import random
import timeit
import math
import numpy
data = [((random.random()*100, random.random()*100), (random.random()*100, random.random()*100)) for i in range(100000)]
base = """
import random
data = {0}
""".format(data)
first = base + """
import math
def distance(pA, pB):
return math.sqrt((pA[0] - pB[0]) ** 2 + (pA[1] - pB[1]) ** 2)
"""
second = base + """
import numpy
def distance(pA, pB):
pt_1 = numpy.array((pA[0], pB[1]))
pt_2 = numpy.array((pA[0], pB[1]))
return numpy.linalg.norm(pt_1-pt_2)
"""
execute = """
distance(*data.pop())
"""
print('Finished preparing.')
print('Executing test 1')
f = timeit.repeat(execute, first, repeat=3, number=100000)
print('Executing test 2')
s = timeit.repeat(execute, second, repeat=3, number=100000)
print('Math module: maximmum {0}; minummum {1}; average {2}'.format(max(f), min(f), numpy.mean(f)))
print('Numpy module: maximmum {0}; minummum {1}; average {2}'.format(max(s), min(s), numpy.mean(s)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment