Skip to content

Instantly share code, notes, and snippets.

Embed
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(i_counter)
print("Starting at: " + str(datetime.now()))
# 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(datetime.now()))
print("Total seconds:" + str(seconds))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.