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

Question:

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.

Asked By: Jeremy Collprav

||

Answers:

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

Answered By: blechdavier
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.