1# Compute the Mandelbrot set, to test complex numbers
2
3
4def mandelbrot(w, h):
5    def in_set(c):
6        z = 0
7        for i in range(32):
8            z = z * z + c
9            if abs(z) > 100:
10                return i
11        return 0
12
13    img = bytearray(w * h)
14
15    xscale = (w - 1) / 2.4
16    yscale = (h - 1) / 3.2
17    for v in range(h):
18        line = memoryview(img)[v * w : v * w + w]
19        for u in range(w):
20            c = in_set(complex(v / yscale - 2.3, u / xscale - 1.2))
21            line[u] = c
22
23    return img
24
25
26bm_params = {
27    (100, 100): (20, 20),
28    (1000, 1000): (80, 80),
29    (5000, 1000): (150, 150),
30}
31
32
33def bm_setup(ps):
34    return lambda: mandelbrot(ps[0], ps[1]), lambda: (ps[0] * ps[1], None)
35