Take the 2-minute tour ×

How should I measure number of seconds passed in a infinite loop? I'm using Vpython, and in my loop i change a position of some elements etc. The thing is that i have to change color of lights(sphere) every X seconds and it all happens in infinite loop.

while True:
    #some stuff
    'here i have to count those seconds'
    #some other stuff
share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

You could determine the elapsed time by calling time.time(), and taking the difference.


Say you last changed the colour at time T. Keep calling time.time() inside the loop, and change the colour again when time.time() - T exceeds X.

share|improve this answer
Ok, but with what function i should initiate the first time? –  Emil Smęt Jan 9 '13 at 17:13
It works now, I'll try though mgilson way as well and see which one is better. –  Emil Smęt Jan 9 '13 at 17:18
add comment

You should probably use timeit.default_timer() to figure out what time it is and compute the difference.


from timeit import default_timer
clock_start = default_timer()
#... do something here
clock_end = default_timer()
print "This took %s seconds" %( clock_end - clock_start )
share|improve this answer
+1 for default_timer –  tom10 Jan 9 '13 at 17:11
add comment

Your Answer


