In Python3, I find that if I replace a
for i in range(n):
statement with
while i < n:
I get significant runtime gains. My loop itself is not meaty where it does couple of basic arithmetic operations.
Any pointers to why I am seeing this behavior?
Edit: n is ranging in 10s of K, 10K, 12K etc Timing I observe is .19s for 12K, .12s for 10K with while loop. Whereas with 'while' loop, i see .11s for 12K, .08s for 10K. Here is my program:
target = 0 i = 1 #for i in range(1, n+1): while i < n+1: target += i * (2 ** (i - 1)) + (i * (i + 1))//2 i += 1 return target % (10 ** 9 + 7)