banner

© 2004-20 ict4u.net

Reaction Timer

Code snippet to time the execution of a piece of code

import time
startTime =time.time()

repsonse = input("Press any key")

elapsedTime = time.time() - startTime
mSec = elapsedTime*1000
if mSec >1 :
	# minutes and seconds
	print("Elapsed time:{:02d}:{:02.0f}".format(int(elapsedTime / 60) ,elapsedTime ))
else:
	# only milliseconds
	print ("Elapsed time: {:0.5f}ms".format( mSec))