Comparing while loop and for loop in python

Using following code i found for loop is faster in python

import time                                                                                  

=

st = time.time()
while i < 100_000_000:
a = 0
i += 1
print(time.time()-st)

i = 0
st = time.time()
while True:
a = 0
if i < 100_000_000:
i += 1
else:
break
print(time.time()-st)

st = time.time()
for i2 in range(100_000_000):
a = 0
print(time.time()-st)

Results
E:\Anaconda\python.exe E:/Projects/Test/t.py
14.323773622512817
14.415262937545776
9.459656000137329

Process finished with exit code 0

No comments:

Post a Comment

Building a CLI-Based People Tracking and Dwell Time Analytics System Using YOLOv8 and DeepSORT

  Introduction Tracking people across video frames and analyzing their behavior (like  dwell time ) is a crucial task for many real-world ap...