-
Notifications
You must be signed in to change notification settings - Fork 6
/
mandelbrot_cython.pyx
54 lines (40 loc) · 1.24 KB
/
mandelbrot_cython.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# The Computer Language Benchmarks Game
# http://shootout.alioth.debian.org/
#
# contributed by Robert Bradshaw
import sys
def main(int size, outfile=sys.stdout):
cdef int i, x, y
cdef double step = 2.0 / size
cdef double Cx, Cy, Zx, Zy, Tx, Ty
cdef line = ' ' * ((size+7) // 8)
cdef char *buf = line
cdef unsigned char byte_acc
write = outfile.write
write("P4\n%s %s\n" % (size, size))
for y in range(size):
byte_acc = 0
for x in range(size):
i = 50
Zx = Cx = step*x - 1.5
Zy = Cy = step*y - 1.0
Tx = Zx * Zx
Ty = Zy * Zy
while True:
# Z = Z^2 + C
Zx, Zy = Tx - Ty + Cx, Zx * Zy + Zx * Zy + Cy
Tx = Zx * Zx
Ty = Zy * Zy
i -= 1
if (i == 0) | (Tx + Ty > 4.0):
break
byte_acc = (byte_acc << 1) | (i == 0)
if x & 7 == 7:
buf[x >> 3] = byte_acc
if size & 7 != 0:
# line ending on non-byte boundary
byte_acc <<= 8 - (size & 7)
buf[size >> 3] = byte_acc
write(line)
if __name__ == '__main__':
main(int(sys.argv[1]), sys.stdout)