0

Say I have some simple code such as the following:

import time

tic = time.perf_counter()

for x in range(1000):
    print(str(x))

toc = time.perf_counter()
print(toc - tic)

It prints 1000 things, and then prints the time it took to do it. When I run this, I get:

0
1
2
3
4
5
6

… skipped for brevity …

995
996
997
998
999
2.0521691879999997

A little more than two seconds. Not bad.

Now say I wanted to only have a single number showing at a time. I could do :

import time

tic = time.perf_counter()

for x in range(1000):
    print('\r' + str(x), end = '')

toc = time.perf_counter()
print()
print(toc - tic)

I get (at the very end)

999
0.46631713999999996

This seems weird, because in the second script, you’re printing more things (some \r’s and x) than the first script (just x). So why would it be faster?

But in the second script, the output is very un-smooth. It looks like the program is counting by 110s. Why doesn’t it just start spitting out numbers rapidly like in the first one?

By the way, apparently sys.stdout.write() is faster than print(), and with the former, both the first script and the second script are about the same speed, but the second script is still not smooth.

joyoforigami
  • 138
  • 8
  • The difference is probably due to not have to scroll all the existing line when outputing the `\r`. – martineau Jun 29 '22 at 02:07
  • Does the difference stay if you disable [output buffering](https://stackoverflow.com/questions/107705/disable-output-buffering)? Python is line-buffered by default when writing to a console, and the definition of "line" doesn't include `\r`. – Nick ODell Jun 29 '22 at 02:11
  • That `'\r' * len(...)` is odd - why would you want to put out more than 1 return? Is this a holdover from some earlier version using backspacing? – access violation Jun 29 '22 at 02:12
  • @accessviolation I did not realize you could use only one /r. Thank you for the tip. – joyoforigami Jun 29 '22 at 02:22
  • @NickODell Both differences are still the same – joyoforigami Jun 29 '22 at 02:25
  • How do you run this? In some IDE or from the command line, for example? – access violation Jun 29 '22 at 10:34
  • Btw: *you’re printing more things (some \r’s and x)* -- now not true. In the first case, each time, you print the digits, followed by newline. In the second case, you print carriage return, followed by the digits. (The newline probably turns into carriage return, linefeed, so the second case has 1000 fewer characters actually output, but I don't think that accounts for the speedup seen). – access violation Jun 29 '22 at 10:39
  • @accessviolation not the interactive IDE, I just created a new file and ran it. – joyoforigami Jun 29 '22 at 17:25
  • @martineau if so, why does the output come out in chunks? shouldn’t it just smoothly spit out the numbers? – joyoforigami Jul 11 '22 at 18:51
  • Because, as @Nick already said, output is line buffered by default. – martineau Jul 11 '22 at 19:00
  • @martineau Oh. I understand now. Should I close this question? – joyoforigami Jul 11 '22 at 19:01

0 Answers0