0

Consider the following code:

import time
import os
for a in range(10, 0, -1): # total time is actually 1 second. no float allowed for arg 3.
    print(a)
    time.sleep(0.X) #unknown time...
    os.system('cls')

At start of program, a is reduced by 1/10 of a second at a time.

Problem: The os.system('cls') takes more than 0.1 seconds to process. Any solution to make it faster?

user12055579
  • 694
  • 11
  • 22
  • 1
    Why do you have a variable as an int both outside your for loop and inside it (as a counter) (the loop variable will override the outside `a` variable)? The above for loop won't run as well because your `(10, 0, 1)` means "start at 10, stop at 0, and step each by 1". I think you meant -1? – user12055579 Apr 04 '20 at 02:03
  • 2
    How long is the `time.sleep` going to sleep for (in seconds)? Why is that unknown? – user12055579 Apr 04 '20 at 21:26
  • @user12055579 Time.sleep(x) is unknown because I do not know how long os.system(cls) takes to process. I only know that it's more than one ms because the milliseconds where not milliseconds –  Apr 04 '20 at 22:21
  • 2
    You aren't going to make clearing the screen (or printing) instantaneous, so if you want this loop to run every tenth of a second then you should use a proper clock rather than trying to make your own clock with `time.sleep`. [This other Q&A](https://stackoverflow.com/q/43892334/12299000) has some examples. – kaya3 Apr 04 '20 at 22:27
  • @kaya3 Is it ok if you can reformulate this into an answer? It would be appreciated! Thanks! –  Apr 04 '20 at 22:32

2 Answers2

1

Actually, you can make the cmd window smaller to take less processing time. It works! Having less pixels to generate and clear makes it an ideal option.

def count():
    for a in range(10, 0, -1):
        print(a)
        time.sleep(0.1) #I know I am breaking the DO NOT USE TIME.SLEEP rule.
        os.system('cls')
count()

As said above, it's never reliable to use time.sleep, but if you shrink the window, it comes close. (I think it doesn't have to be spot on.)

0

I understand that you're problem is that the for loop runs for more than 1 second because os.system('cls') is not instantaneous:

You can test this by using the timeit module in Python:

Without os.system('cls'):

import time
import timeit
import os
start = timeit.default_timer()
for a in range(10, 0, -1): # total time is actually 1 second. no float allowed for arg 3.
    print(a)
    time.sleep(0.1) #unknown time...
stop = timeit.default_timer()
print('Time: ', stop - start)  

Output:

10
9
8
7
6
5
4
3
2
1
Time:  1.0021432

With os.system('cls'):

Time:  1.7151739000000001

You cannot solve this because even without os.system('cls') it takes more than 1 second. The function os.system('cls') itself already takes about 0.1649219 seconds to execute. You cannot make Python library function faster.

user12055579
  • 694
  • 11
  • 22
  • @ user 12055579 This is helpful, its that I am running this in cmd and I would like to clear screen to display a different output each time. Thanks anyway! –  Apr 05 '20 at 00:03
  • OK, this is a bit late, but I actually found my own solution: make the cmd window smaller. Weird. –  Apr 07 '20 at 16:22