How to render Mandelbrot Set faster?

前端 未结 6 1520
臣服心动
臣服心动 2020-12-20 07:34

I\'m currently drawing the Mandelbrot set pixel by pixel with PhotoImage and tkinter. I\'m using basically the algorithm directly with no modifications. Are there methods to

6条回答
  •  星月不相逢
    2020-12-20 08:01

    For a modest increase in speed (but not enough to offset the difference between a compiled language and an interpreted one), you can precalculate some of the values.

    Right now, you're calculating DIAMETER / HEIGHT once per inner loop, and CENTER[1] - 0.5 * DIAMETER as well as DIAMETER / WIDTH once per outer loop. Do this beforehand.

    len(colors) also won't change and can be replaced by a constant. In fact, I'd probably write that function as

    def color(i):
        if i == ITERATIONS:
            return "#000000"
        else:
            return ("#0000AA", "#88DDFF", "#FF8800", "#000000")[(i//2) % 4]
            # are you sure you don't want ("#0000AA", "#88DDFF", "#FF8800")[(i//2) % 3] ?
    

    Also, x**2 is slower than x*x (because the x**y operator doesn't shortcut for the trivial case of y==2), so you can speed that calculation up a bit.

提交回复
热议问题