Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import time
- import random
- def find_dist_squared(x1, y1, x2, y2):
- return (x2 - x1) ** 2 + (y2 - y1) ** 2
- def find_dist_squared_v2(x1, y1, x2, y2):
- dx = x2 - x1
- dy = y2 - y1
- return dx * dx + dy + dy
- def find_dist(x1, y1, x2, y2):
- return math.hypot(x1 - x2, y1 - y2)
- def time_function_10000_times(func, name):
- start = time.clock()
- for _ in range(10000):
- func()
- elapsed = time.clock()
- elapsed = elapsed - start
- print ("Time spent in " + name + " is: " + str(elapsed))
- def find_dist_squared_impl():
- return find_dist_squared(0, 0, random.random(), random.random())
- def find_dist_squared_v2_impl():
- return find_dist_squared_v2(0, 0, random.random(), random.random())
- def find_dist_impl():
- return find_dist(0, 0, random.random(), random.random())
- time_function_10000_times(find_dist_impl, "find dist")
- time_function_10000_times(find_dist_squared_impl, "find dist squared")
- time_function_10000_times(find_dist_squared_v2_impl, "find dist squared v2")
- """
- Time spent in find dist is: 0.005446
- Time spent in find dist squared is: 0.005252
- Time spent in find dist squared v2 is: 0.004622
- Process finished with exit code 0
- """
Add Comment
Please, Sign In to add comment