I've ran this code with your suggestions (all very clever ones, thank you!!) to try and see what's faster. The differences are actually quite impressive:
import time
def whosfaster(a, b):
start_time = time.time()
maximum = max(a, b)
minimum = min(a, b)
method0 = time.time() - start_time
start_time = time.time()
if a > b:
maximum, minimum = a, b
else:
maximum, minimum = b, a
method1 = time.time() - start_time
start_time = time.time()
maximum, minimum = max(a, b), min(a, b)
method2 = time.time() - start_time
start_time = time.time()
maximum, minimum = (a, b) if a > b else (b, a)
method3 = time.time() - start_time
start_time = time.time()
if max(a, b) == a:
maximum = a
minimum = b
else:
maximum = b
minimum = a
method4 = time.time() - start_time
return maximum, minimum, method0, method1, method2, method3, method4
total = [0, 0, 0, 0, 0]
for a in range(1, 5000):
for b in range(1, 5000):
maximum, minimum, method0, method1, method2, method3, method4 = whosfaster(a, b)
total[0] += method0
total[1] += method1
total[2] += method2
total[3] += method3
total[4] += method4
print(total)
The results I obtained are:
Run number 1 :
[7.562410593032837, 2.4336302280426025, 7.17744255065918, 3.160186529159546, 5.583981037139893]
Run number 2 but my laptop was on battery saver mode :
[12.757804155349731, 4.507119178771973, 13.357162237167358, 5.251198768615723, 8.934610605239868]
If someone wants to run it on their end to compare, you're welcome to do so, my laptop isn't the fastest!!
EDIT : I didn't really know how to implement Mark Ransom's answer into practice, so I didn't try it. Feel welcome to edit my post and change my code and to rerun it on your hand if you know how to do so.