How to time how long it took to run a piece of code in python. Preferably with millisecods of accuracy.

I need to run a loop with fairly accurate timing. My plan is to add a padding delay based off of how long it took to run the calculations of the loop.

here, a couple of alternatives.

# Brain should be defined by default
brain=Brain()

brain.screen.print("Hello EXP")


start_time = brain.timer.system()
# do something
for i in range(10):
    sleep(10)
end_time = brain.timer.system()
print('delta ', end_time - start_time )

# another way
my_timer = Timer()

my_timer.reset()
# do something
for i in range(10):
    sleep(10)
print('delta ', my_timer.time() )
3 Likes