Hey everyone!
Recently I was playing with python. And noticed in my opinion strange behaviour.
I have 4 functions:
def t1():
s = time.perf_counter_ns()
counter = 0
for _ in range(10_000_000):
counter += 1
e = time.perf_counter_ns()
print(counter)
return int((e - s) / 1_000_000)
def t2():
s = time.perf_counter_ns()
counter = sum(1 for _ in range(10_000_000)
e = time.perf_counter_ns()
print(counter)
return int((e - s) / 1_000_000)
def t11():
s = time.perf_counter_ns()
counter = 0
for i in range(10_000_000):
counter += i
e = time.perf_counter_ns()
print(counter)
return int((e - s) / 1_000_000)
def t22():
s = time.perf_counter_ns()
counter = sum(range(10_000_000))
e = time.perf_counter_ns()
print(counter)
return int((e - s) / 1_000_000)
When I run t11 and t22 I get that time t11 > t22. But when I run t1 and t2 I get the opposite.
I tried to run it on different machines - Intel and AMD, both are Win11 with Python 3.11.0 and get the same result in both cases. Below is result for AMD run.
So, I just wonder why this is happening.