I tested performance for in loop in python.
It contains just loop and plus operations.
But it takes about 0.5secs. How can I do it more faster?
import time
start_time = time.time()
val = -1000000
for i in range(2000000):
val += 1
elapsed_time = time.time() - start_time
print(elapsed_time) # 0.46402716636657715
timeitmodule to do time trials; it uses more accurate timings and tries to avoid garbage collection and OS scheduler pitfalls (by repeating the experiment).val = 1000000. This is probably the worst case of premature optimisation I've ever seen!range()with a big number + performance of the iteration on whatrange()returned for your Python version + performance of integer addition. FWIW in Python 2.x,range()returns alist, so you're testing the performance of creating alistof 2000000 integers...