Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix mandelbrot benchmark.

  • Loading branch information...
commit 2f44251776561ee934f5a9a06f408be95449014b 1 parent fb0ba42
Robert Bradshaw robertwb authored
Showing with 21 additions and 17 deletions.
  1. +21 −17 mandelbrot_cython.pyx
38 mandelbrot_cython.pyx
View
@@ -3,48 +3,52 @@
#
# contributed by Robert Bradshaw
-# Doesn't quite match the test output. Numerical noise?
-
import sys
def main(int size, outfile=sys.stdout):
+
cdef int iter = 50
- cdef double step = 2.0 / size
- cdef double Cx, Cy, Zx, Zy
cdef int i, xi, yi
- cdef double square_abs
+ 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
+ cdef unsigned char byte_accumulate
write = outfile.write
write("P4\n%s %s\n" % (size, size))
-
for yi in range(size):
- byte_acc = 0
+
+ byte_accumulate = 0
+
for xi in range(size):
+
+ i = iter
Zx = Cx = step*xi - 1.5
Zy = Cy = step*yi - 1.0
- i = iter
+
+ Tx = Zx * Zx
+ Ty = Zy * Zy
while True:
- # Soon (hopefully) Cython will have native c complex types
# Z = Z^2 + C
- Zx, Zy = Zx*Zx - Zy*Zy + Cx , 2*Zx*Zy + Cy
- square_abs = Zx*Zx + Zy*Zy
+ Zx, Zy = Tx - Ty + Cx , Zx * Zy + Zx * Zy + Cy
+ Tx = Zx * Zx
+ Ty = Zy * Zy
i -= 1
- if (i == 0) | (square_abs > 4.0):
+ if (i == 0) | (Tx + Ty > 4.0):
break
- byte_acc = (byte_acc << 1) | (square_abs < 4.0)
+ byte_accumulate = (byte_accumulate << 1) | (i == 0)
if xi & 7 == 7:
- buf[xi >> 3] = byte_acc
+ buf[xi >> 3] = byte_accumulate
+ byte_accumulate = 0 # TESTING
if size & 7 != 0:
# line ending on non-byte boundary
- byte_acc <<= 8 - (size % 8)
- buf[size >> 3] = byte_acc
+ byte_accumulate <<= 8 - (size & 7)
+ buf[size >> 3] = byte_accumulate
write(line)
Please sign in to comment.
Something went wrong with that request. Please try again.