Is setting `turtle.speed(0)` necessary when we have turtle.tracer(0)

若如初见. 提交于 2020-07-22 05:59:39

问题


Is there a difference between:

import turtle
turtle.tracer(0)
turtle.speed(0)
while True:
    turtle.goto(turtle.xcor()+1)
    turtle.update()

And:

import turtle
turtle.tracer(0)
while True:
    turtle.goto(turtle.xcor()+1)
    turtle.update()

I've heard that setting turtle.speed(0) make things faster, but if so, I don't see any difference.


回答1:


According to: https://www.eg.bucknell.edu/~hyde/Python3/TurtleDirections.html

The tracer() method:

  • Can be used to accelerate the drawing of complex graphics.

  • Turning the turtle off makes the turtle disappear and makes drawing MUCH faster.

And the official documentation https://docs.python.org/2/library/turtle.html#turtle.speed says:

  • “fastest”: 0 # For setting the turtle speed

CONCLUSION: I think it is fair to say that:

If you are doing very complex graphics, and you want speed, use both and in complex graphics you should see some sort of difference as they both speed up the turtle

If you are doing simple graphics, the speed from using both will not as noticeable than if you did a complex one, so it would not be necessary to use both but only use, for example: turtle.speed(0) As according to the official documentation: https://docs.python.org/2/library/turtle.html#turtle.tracer tracer() has been deprecated in newer versions of the turtle module.

So I would suggest changing the speed first, and then if you need faster performance change the tracer. Or you can just disable the tracer because you do not want the turtle animation to show, really it is up to you, but I hope I could have guided you.




回答2:


My experience writing my own code, and rewriting other people's, is that with tracing turned off, tracer(0), the Turtle.speed() method makes no difference. The turtle.py source code appears to echo this:

if self._speed and screen._tracing == 1:

Only if we're tracing, tracer(1), do we even consider the turtle's speed.

Furthermore, my experience has been that turning off tracing has more effect than turtle.speed('fastest') as the speed() method only affects individual turtles whereas tracer(0) affects all the turtles and potentially other actions.

My own rules of thumb with respect to tracer():

  • Use update() to show the user the current graphic state, avoid turning tracer() on and off for this purpose.

  • Don't assume your update() calls are the only ones. Some turtle operations call update() so don't be surprised.

  • Don't turn tracing off until your code is basically working -- i.e. don't debug in the dark.

  • If you're not using events (e.g. mouse, keyboard, timer), then turn tracing back on just before the final mainloop() call or equivalent. Otherwise late actions like hiding the turtle won't appear to happen.

  • Don't use any argument other than 0 and 1 (or False and True.) Setting a numeric value to force updates every Nth action is tricky to work out and usually wrong.

  • If you turn off tracing, you can toss your speed() calls.



来源:https://stackoverflow.com/questions/62542427/is-setting-turtle-speed0-necessary-when-we-have-turtle-tracer0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!