1- from mandel import mandel
1+ #And for desert:
2+ import sys
3+ print (sys .executable )
4+ from timeit import default_timer as timer
5+ import cffi
6+ from PIL import Image
27
3- def create_fractal (image , width , height , iters ):
4- pixel_size_x = 3.0 / width
5- pixel_size_y = 2.0 / height
6- for y in range (height ):
8+ ffi = cffi .FFI ()
9+
10+ class Img (object ):
11+ def __init__ (self , width , height ):
12+ self .width = width
13+ self .height = height
14+ self .data = ffi .new ('uint8_t[%d]' % (width * height ,))
15+
16+ def create_fractal (image , iters , func , oneval ):
17+ ''' Call a function for each pixel in the image, where
18+ -2 < real < 1 over the columns and
19+ -1 < imag < 1 over the rows
20+ '''
21+ pixel_size_x = 3.0 / image .width
22+ pixel_size_y = 2.0 / image .height
23+ for y in range (image .height ):
724 imag = y * pixel_size_y - 1
8- yy = y * width
9- for x in range (width ):
25+ yy = y * image . width
26+ for x in range (image . width ):
1027 real = x * pixel_size_x - 2
11- color = mandel (real , imag , iters )
12- image [yy + x ] = color
28+ func (real , imag , iters , oneval )
29+ image .data [yy + x ] = oneval [0 ]
30+
31+ def mandel (x , y , max_iters , value ):
32+ """
33+ Given the real and imaginary parts of a complex number,
34+ determine if it is a candidate for membership in the Mandelbrot
35+ set given a fixed number of iterations.
36+ """
37+ i = 0
38+ c = complex (x ,y )
39+ z = 0.0j
40+ for i in range (max_iters ):
41+ z = z * z + c
42+ if (z .real * z .real + z .imag * z .imag ) >= 4 :
43+ value [0 ] = i
44+ return 0
45+ value [0 ] = max_iters
46+ return max_iters
47+
48+ # Pure python
49+ width = 1500
50+ height = 1000
51+ image = Img (width , height )
52+ s = timer ()
53+ oneval = ffi .new ('uint8_t[1]' )
54+ create_fractal (image , 20 , mandel , oneval )
55+ e = timer ()
56+ pure_pypy = e - s
57+ print ('pure pypy required {:.2f} millisecs' .format (pure_pypy ))
58+ im = Image .fromarray (image .data .reshape (height , width ))
59+ im .save ('pypyy.png' )
0 commit comments