9

If I have a list l:

l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Is there a way to control the following for loop so that the next element in the list is only printed one second after the previous?

for i in l:
    print i

In other words, is there a way to elegantly slow down a loop in Python?

4
  • 11
    That's funny, most people want to know how to speed up their loops! Commented May 15, 2013 at 1:01
  • 1
    I find that running it on my old Treo 300 is a good way to slow down the loops… Commented May 15, 2013 at 1:07
  • More seriously, if you're trying to watch what's going on, you may want to try running in a debugger or an interactive visualizer, instead of just tossing in sleep calls. Commented May 15, 2013 at 1:11
  • How can slow be achieved without time.sleep? (Am on a platform that allows time but not sleep, a security fear with them). Pretty sure an inconsistency I'm seeing is from something asynchronous on the back end and I have no access and they're no help..I think the bug I'm seeing will disappear with slower execution. Commented Oct 19, 2017 at 4:19

5 Answers 5

19

You can use time.sleep

import time

for i in l:
    print i
    time.sleep(1)
Sign up to request clarification or add additional context in comments.

Comments

10

If you use time.sleep(1), your loops will run a little over a second since the looping and printing also takes some time. A better way is to sleep for the remainder of the second. You can calculate that by using -time.time()%1

>>> import time
>>> L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> for i in L:
...     time.sleep(-time.time()%1)
...     print i
... 

It's easy to observe this by using print i, repr(time.time())

>>> for i in L:
...     time.sleep(-time.time()%1)
...     print i, repr(time.time())
... 
0 1368580358.000628
1 1368580359.001082
2 1368580360.001083
3 1368580361.001095
4 1368580362.001149
5 1368580363.001085
6 1368580364.001089
7 1368580365.001086
8 1368580366.001086
9 1368580367.001085

vs

>>> for i in L:
...     time.sleep(1)
...     print i, repr(time.time())
... 
0 1368580334.104903
1 1368580335.106048
2 1368580336.106716
3 1368580337.107863
4 1368580338.109007
5 1368580339.110152
6 1368580340.111301
7 1368580341.112447
8 1368580342.113591
9 1368580343.114737

4 Comments

Besides forcing this to align on the exact second mark, this also assumes that the cost of time.time and time.sleep is negligible, but the time of print is significant, and that sleep is accurate to the relevant scale, both of which are unlikely to be true on most platforms…
@abarnert, the time caused from time.time() and time.sleep() doesn't matter much as long as it's fairly consistent. sleep() doesn't offer a guarantee for precise timing anyway.
The second part of your comment is the main point. sleep isn't accurate or precise no matter what you do. It could easily sleep past the next .000 mark, which means your code will lose a whole second, while the "dumb" code would just lose 50us. Is that really an improvement? You need more complicated logic (basically, keep track of the time to sleep until, so you can correct for oversleep) to actually do this right. There are very few situations where sleep(1) is not good enough, but sleep(-time.time()%1) is.
@abarnert, depends on the use case. If the code in the loop is more complicated than print and takes a substantial part of (but less than) a second, my version will run 3600 times an hour.
5

You can pause the code execution using time.sleep:

import time

for i in l:
    print(i)
    time.sleep(1)

Comments

3

Use the time.sleep function. Just do time.sleep(1) in your function.

Comments

-6

Use time.sleep(number):

for i in range(i)
print(1)
time.sleep(0.05)

1 Comment

Necro-posting is fine, if it brings some additional value, but this answer brings no such light on the matter: the code doesn't even work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.