Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import struct
- DEPTH = 255
- mandel_lambda = lambda V, B, c: c and mandel_lambda(V*V + B, B, c - 1) if (abs(V) < 6) else (2 + c- 4*abs(V)**-0.4) / DEPTH
- def mandel_recursive(V, B, c):
- if c == 0:
- return 0
- if abs(V) < 6:
- return mandel_recursive(V**2 + B, B, c-1)
- else:
- return (2 + c- 4*abs(V)**-0.4) / DEPTH
- def mandel(z0, c, itr):
- z = z0
- for i in range(DEPTH - 1, -1, -1):
- z = (z**2) + c
- if abs(z) > 6:
- return (2 + i - 4 *abs(z)**-0.4) / DEPTH
- return 0
- def get_colour(total):
- r = (total * 80) + ((total**9) * DEPTH) - (950 * (total**99))
- g = (total * 70) - (880 * (total**18)) + (701 * (total**9))
- b = total * (DEPTH**(1 - (total**45) * 2))
- return r, g, b
- v, x = 1500, 1000;
- # For information regarding the BMP format, see:
- # http://en.wikipedia.org/wiki/BMP_file_format
- if __name__ == '__main__':
- tests = [(mandel, 'mandelbrot_basic'),
- (mandel_recursive, 'mandelbrot_recursive'),
- (mandel_lambda, 'mandelbrot_lambda'),
- ]
- for func, name in tests:
- fname = '%s.bmp' % name
- print "Writing %dx%d image to: %s" % (v, x, fname)
- start = time.time()
- with open(fname, 'wb') as fp:
- fmt = '<QIIHHHH' # little-endian: unsigned long long, unsigned int (2), unsigned short (4)
- fp.write('BM' + struct.pack(fmt, # 'BM' == Windows bitmap identifier
- v * x * 3 + 26, # unsigned long long record size (+4 reserved bytes)
- 26, 12, # unsigned int(starting offset)
- v, x, # unsigned short(width, height)
- 1, 24)) # unsigned short(planes, bits per pixel)
- for X in range(v * x):
- total = 0
- for A in range(9):
- row = X % v
- off = (A % 3) / 3.0
- real = off + row
- imag = (X/v + off - x/2) / 1j
- c = ((real + imag) * (2.5 / x)) - 2.7
- esc = func(0, c, DEPTH)
- total += esc**2
- colour = get_colour(total / 9)
- fp.write(struct.pack('BBB', *colour))
- print "Done. %0.3f seconds." % (time.time() - start)
- # Results on a Intel Core2 6400 @ 2.13 GHz:
- #
- # pypy-1.6.0
- # Writing 1500x1000 image to: mandelbrot_basic.bmp
- # Done. 160.632 seconds.
- # Writing 1500x1000 image to: mandelbrot_recursive.bmp
- # Done. 188.082 seconds.
- # Writing 1500x1000 image to: mandelbrot_lambda.bmp
- # Done. 132.598 seconds.
- #
- # python-2.6.5
- # Writing 1500x1000 image to: mandelbrot_basic.bmp
- # Done. 513.069 seconds.
- # Writing 1500x1000 image to: mandelbrot_recursive.bmp
- # Done. 661.153 seconds.
- # Writing 1500x1000 image to: mandelbrot_lambda.bmp
- # Done. 572.474 seconds.
- #
- # Don't take these numbers too seriously.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement