3

I've found that python's turtle implementation is amazingly slow. I expected some delay, but not minutes of waiting for a relatively simple fractal (Koch curve with 5 iterations).

Even after setting turtle.speed(0) it is still very slow. Maybe there's a bug since it's not instantaneous like claimed in the docs?

This answer suggested re-implementing turtles by hiding a window. A similar question got no answer. Am I missing something, or is re-implementing the way to go?

Here is my stripped down code (the creation of the l-system is almost instantaneous):

import turtle

def l_system(V, w, P, n):
    current = w

    for i in range(n):
        current = [P[x] if x in P else x for x in list(current)]
        current = ''.join(current)

    return current

def run_turtle(var, start, rules, iters, angle, size, scale):
    terry = turtle.Turtle()
    terry.pensize(1)
    terry.pencolor("blue")
    terry.speed(0)

    dist = size / ((iters + 1) ** scale)
    positions = []
    angles = []

    instructions = l_system(var, start, rules, iters)

    for instr in instructions:
        if instr in ('F', 'G'):
            terry.forward(dist)

        elif instr in ('M', 'N'):
            terry.penup()
            terry.forward(dist)
            terry.pendown()

        elif instr == '[':
            positions.append(terry.pos())
            angles.append(terry.heading())

        elif instr == ']':
            terry.goto(positions.pop())
            terry.setheading(angles.pop())

        elif instr == '+':
            terry.left(angle)

        elif instr == '-':
            terry.right(angle)

    turtle.mainloop()

def right_koch():
    run_turtle(('F',), 'F', {'F':'F+F-F-F+F'}, 5, 90, 500, 3)

right_koch()
Community
  • 1
  • 1
qwr
  • 9,525
  • 5
  • 58
  • 102
  • Your question doesn't have the information we'd need to tell why your program is slow, so there isn't much we can do to help you. – user2357112 Aug 28 '14 at 06:20
  • @user2357112 I'm not asking about any specific program, just python's turtles drawing anything in general. – qwr Aug 28 '14 at 06:21
  • 1
    What do you expect us to say? "Yes, `turtle` is slow" or "No, `turtle` isn't slow"? We can't tell what you might or might not be missing, since we can't tell what you've done. – user2357112 Aug 28 '14 at 06:22
  • @user2357112 In fact, yes. Or something along the lines of "tkinter is slow in general so it is impractical to use for fractals" or "tkinter is fast enough to generate fractals". I'm currently editing my question to include code. – qwr Aug 28 '14 at 06:24
  • @CommuSoft Hm, can you give me an example of a package that is similar to turtles? Or do you think I should re-implement it myself? – qwr Aug 28 '14 at 06:42

3 Answers3

17

Turn off the drawing delay:

turtle.delay(0)

and hide the turtle:

terry.ht()

Turning off the drawing delay is the big one. If you don't do that, there's a 10-millisecond pause whenever the turtle moves.


If you want it to go even faster, and you only care about the finished picture, you can turn off screen updates entirely:

turtle.tracer(0, 0)

and call update a single time when your turtle has executed all its commands:

terry.update()

With tracing off and a manual update call, the program finishes near-instantaneously on my machine.

user2357112
  • 260,549
  • 28
  • 431
  • 505
2

Here’s a quick copy-paste for anyone looking. Credits to @user2357112

Use these methods to greatly speed things up and get right to the end result:

.speed(0)
.delay(0)
.ht()
.tracer(0, 0)

# Code goes here

.update()
Cloud
  • 938
  • 1
  • 8
  • 24
0

This is even faster for copy-pasting:

from turtle import Turtle, speed, delay, tracer, update, done
def initialize():
 speed(0)
 delay(0)
 tracer(0,0)
 turt = Turtle()
 turt.ht()
 return turt
def finish():
 update()
 done()

Everything comes from @user2357112.

Number Basher
  • 109
  • 2
  • 10