有什么方法可以加快 Python 乌龟超过速度 (0) 的速度吗?

Is there any way to speed up Python turtle past speed(0)?

我正在 Python 中编写代码,使用 turtle 来可视化冒泡排序,但速度非常慢。有什么办法可以加快它的速度,因为 speed(0) for Python turtle 不够快。或者还有其他方法可以使用 Python 来完成此类项目吗?

您应该做的第一件事是确认是海龟图形导致速度变慢。例如,运行 带图形和不带图形的冒泡排序,以查看实际损失了多少时间。

加速海龟图形的一种方法是使用 tracer():

screen = turtle.Screen()
# ...
screen.tracer(False)  # turn off graphic updates
# ...
# Whenever you make a change you want the user to see:
screen.update()  # make screen current
# ...
# When you're completely finished:
screen.tracer(True)  # turn graphics updates back on
# ...
screen.mainloop()

在这种情况下您不需要 speed(0)(又名 speed('fastest')),尽管离开它也没什么坏处。一些图形操作将强制 screen.update() 独立于您对它的调用——不要感到惊讶。确保在最后 tracer(True) 这样隐藏海龟之类的东西才能正常工作。

最后,将代码的最小工作示例添加到上面的问题中,以便进行适当的审查。