Skip to content

Instantly share code, notes, and snippets.

Last active June 28, 2019 13:29
What would you like to do?
from datetime import datetime
import timeit
def loop():
i_counter = 0
# List from 0 to 31999, saved beforehand to not use the generator version of range
# Also reduces the overhead of generating the list twice, which other languages don't have to do
bigrange = list(range(32000))
# No need to save an index variable we never use
for _ in range(10):
for _ in bigrange:
for _ in bigrange:
i_counter = i_counter + 1 if i_counter < 50 else 0
print("Starting at: " + str(
# Using timeit instead of computing the diference in times because it is more pythonic.
# Also coincidentally disables garbage collection
seconds = timeit.timeit('loop()', setup='from __main__ import loop', number=1)
print("Ending at: " + str(
print("Total seconds:" + str(seconds))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment