Skip to content
Advertisement

How do I reduce stutter when repeatedly printing in the console in python?

I have the following sample of code:

import time
# import sys


i = 0
right = True
while True:
    time.sleep(0.1)
    if i == 30:
        right = False
    elif i == 0:
        right = True

    if right:
        i += 1
    else:
        i-= 1

    
    text = ''''''
    for j in range(10):
        text += str("." * int(30-i-j)) + "x" + "n"
    print(text)
    # sys.stdin.write(text)

Video of output

The output is expected, but there is clearly some staggering after each input. Is there any way to reduce this? I’ve already tried to use the apparently more efficient sys.stdin.write, but it doesn’t seem to work with variables as a I get an error with I do.

I am using VS Code for my IDE.

Advertisement

Answer

Have you tried using your OS’s native terminal?

VSCode runs in Electron, so performance would be slower. They did make the terminal rendering faster a while back, but OS terminals are as optimized as it gets afaik, so if you want good performance try that

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement