15 August 2017

Efficient Comparison

My answer: "Option 1 is the most optimal one."

My solution:
import time
L = 1
x = 2
y = 3
R = 9
t1 = time.perf_counter()
def a():
 if L < x ** y <= R:
  return True

t2 = time.perf_counter()
print(t2 - t1)
t3 = time.perf_counter()
def b():
 if x ** y > L and x ** y <= R:
  return True

t4 = time.perf_counter()
print(t4 - t3)
t5 = time.perf_counter()
def c():
 if x ** y in range(L + 1, R + 1):
  return True

t6 = time.perf_counter()
print(t6 - t5)

Output:
5.131850057605017e-07
1.5395550172814964e-06
1.5395550172815045e-05

Previous Next