I'm trying to understand why 1 dedicate python function is slower than manual function.
I create a benchmark with timeit for 2 function.
I loop 100 times.
nb**3
= ~28.166s
nb*nb*nb
= ~10.529s
(using Python 3.5 on windows)
I don't understand why the operator created by python (power operator) take more time than other. The power operator is defined by Python and we dont need to add new function like math.pow
.
def power_test():
nb = 2;
for i in range(100):
b = nb**3
def repeat_test():
nb = 2;
for i in range(100):
b = nb*nb*nb;
if __name__ == '__main__':
import timeit
print("start bench1");
print(timeit.timeit("power_test()", setup="from __main__ import power_test"))
print("end bench1\n");
print("start Bench2");
print(timeit.timeit("repeat_test()", setup="from __main__ import repeat_test"));
print("end bench2\n");
full result:
start bench1
28.166055432365265
end bench1
start Bench2
10.52991797905926
end bench2
Thank you