Created
July 8, 2015 19:55
-
-
Save CrazyPython/ad2a64393f71eb3a7087 to your computer and use it in GitHub Desktop.
This file contains 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
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 |
This file contains 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 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