3

I create a while loop and print the timestamp on every loop. At the beginning I got a performance of about 33 loops by second. However, longer it takes, slower it gets. At 3 minutes, I have a performance of 2 loops by second. I tried to use threading.Timer instead, but it does the same thing. I am aware that Memory and Complexity of calculations can be an issue, but it doesn't seem to be the case in that scenario.

How can I avoid that lack of performance as I want to run the code for hours? Your help would be really appreciated.

import time

while(True):
    print(int(round(time.time() * 1000)))

Output

1556756682157
1556756682216
1556756682240
1556756682269
1556756682296
1556756682324
1556756682358
1556756682387
1556756682415
1556756682441
1556756682470
1556756682501
1556756682556
... // After 3 minutes
1556756860002
1556756860884
1556756861240
1556756861669
1556756862596
1556756863324
1556756863858
1556756864387
8
  • What are you doing in the loop? Commented May 2, 2019 at 0:30
  • It is most probably printing to the console that slows it down. Commented May 2, 2019 at 0:32
  • 1
    How many lines in the console are there at the time of slowing down? Selcuk may be right. I know my Clojure REPL slows to a crawl as soon as I have a few hundred thousand entries. Commented May 2, 2019 at 0:34
  • It is literally the whole code, there is no more process than it. @Selcuk this is what I am thinking too, the queue list of the console print can slow it down I guess. Carcigenicate, one line at the time Commented May 2, 2019 at 0:42
  • On that link they are saying print can considerably slow down too. So I will try to get rid of it and test it. Commented May 2, 2019 at 0:56

1 Answer 1

1

Base on test done here, the print can considerably slow down your loop. Remiove the print and your speed shouldn't decrease that fast anymore. See below the example:

from time import time

start = time()
for i in range(1_000_000):
    print(i)
print(f'run time for printing: {time() - start}')

start = time()
for _ in range(1_000_000):
    pass
print(f'run time for no printing: {time() - start}')

this is what it printed:

# a ton of numbers above this line from printing

run time for printing: 7.047402858734131
run time for no printing: 0.0870048999786377
Sign up to request clarification or add additional context in comments.

Comments

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.