Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

mostly done with mandelbrot

  • Loading branch information...
commit a2922a243c3f77b45775db05ea4b57cec6926e37 1 parent d762231
Robert Bradshaw authored February 10, 2009

Showing 1 changed file with 48 additions and 46 deletions. Show diff stats Hide diff stats

  1. 94  mandelbrot_cython.pyx
94  mandelbrot_cython.pyx
... ...
@@ -1,50 +1,52 @@
1 1
 # The Computer Language Benchmarks Game
2 2
 # http://shootout.alioth.debian.org/
3 3
 #
4  
-# contributed by Tupteq
5  
-# modified by Simon Descarpentries
6  
-
7  
-import sys
8  
-from array import array
9  
-
10  
-def main():
11  
-    cout = sys.stdout.write
12  
-    size = int(sys.argv[1])
13  
-    fsize = float(size)
14  
-    xr_size = xrange(size)
15  
-    xr_iter = xrange(50)
16  
-    bit_num = 7
17  
-    byte_acc = 0
18  
-    result = []
19  
-    local_abs = abs
20  
-
21  
-    cout ("P4\n%d %d\n" % (size, size))
22  
-
23  
-    for y in xr_size:
24  
-        fy = 2j * y / fsize - 1j
25  
-        for x in xr_size:
26  
-            z = 0j
27  
-            c = 2. * x / fsize - 1.5 + fy
28  
-
29  
-            for i in xr_iter:
30  
-                z = z * z + c
31  
-                if local_abs(z) >= 2.: break
32  
-            else:
33  
-                byte_acc += 1 << bit_num
34  
-
35  
-            if bit_num == 0:
36  
-                result.append (byte_acc)
37  
-                bit_num = 7
38  
-                byte_acc = 0
39  
-            else:
40  
-                bit_num -= 1
41  
-
42  
-        if bit_num != 7:
43  
-            result.append (byte_acc)
44  
-            bit_num = 7
45  
-            byte_acc = 0
46  
-
47  
-    cout (array ('B', result).tostring ())
48  
-
49  
-main()
  4
+# contributed by Robert Bradshaw
  5
+
  6
+
  7
+import sys    
  8
+
  9
+def main(int size, outfile=sys.stdout):
  10
+    cdef int iter = 50
  11
+    cdef double step = 2.0 / size
  12
+    cdef double Cx, Cy, Zx, Zy, abs2
  13
+    cdef double Zx2, Zy2
  14
+    cdef int i, xi, yi
  15
+    cdef double square_abs
  16
+    
  17
+    cdef line = ' ' * ((size+7) // 8)
  18
+    cdef char* buf = line
  19
+    cdef unsigned char byte_acc
  20
+    
  21
+    write = outfile.write
  22
+    write("P4\n%s %s\n" % (size, size))
  23
+    
  24
+    
  25
+    for yi in range(size):
  26
+        byte_acc = 0
  27
+        for xi in range(size):
  28
+            Zx = Cx = step*xi - 1.5
  29
+            Zy = Cy = step*yi - 1.0
  30
+            i = 0
  31
+            while True:
  32
+                # Z = Z^2 + C
  33
+                # Soon (hopefully) Cython will have native c complex types
  34
+                Zx, Zy = Zx*Zx - Zy*Zy + Cx , 2*Zx*Zy + Cy
  35
+                square_abs = Zx*Zx + Zy*Zy
  36
+                i += 1
  37
+                if (i > iter) | (square_abs > 4.0):
  38
+                    break
  39
+                    
  40
+            byte_acc = (byte_acc << 1) | (square_abs < 4.0)
  41
+            if xi & 7 == 7:
  42
+                buf[xi >> 3] = byte_acc
  43
+        
  44
+        if size & 7 != 0:
  45
+            # line ending on non-byte boundary
  46
+            byte_acc <<= 8 - (size % 8)
  47
+            buf[size >> 3] = byte_acc
  48
+        write(line)
  49
+
  50
+
  51
+#main()
50 52
 

0 notes on commit a2922a2

Please sign in to comment.
Something went wrong with that request. Please try again.