Mandelbrot Microscope

Discussion in 'Python' started by Nick J Chackowsky, Jul 9, 2004.

  1. Okay, here's my naive solution to coding a Mandelbrot microscope in
    Python... but it's so slow! Would anyone care to comment on how to speed
    this up?


    #This uses the module included with John Zelle's
    #textbook. File available at

    from graphics import *

    DEPTH = 200

    def f(z):
    C = z
    count = 0
    while count < DEPTH and abs(z) < 3.0:
    z = z*z + C
    count += 1
    if count == DEPTH:
    count -= 1
    return count

    def rangecolors(n):
    """ Return a list of nicely blended colors """
    clist = []
    r, g, b = 13, 101, 137
    for c in range(n):
    r += 5
    g += 7
    b += 11
    if r > 255: r = 0
    if g > 255: g = 0
    if b > 255: b = 0
    #end for
    return clist

    def main():
    llx, lly = -3.0, 3.0 #lower left
    urx, ury = 3.0, -3.0 #upper right
    WINDOWSZ = 200
    clist = rangecolors(DEPTH)
    keepgoing = True
    while keepgoing:
    w = GraphWin("Mandelbrot", WINDOWSZ, WINDOWSZ)
    w.setCoords(llx, lly, urx, ury)
    delta = abs(llx-urx)/float(WINDOWSZ)
    re = llx
    while re < urx:
    im = ury
    while im < lly:
    w.plot(re, im, clist[f(complex(re, im))])
    im += delta
    #end while
    re += delta
    #end while
    p1 = w.getMouse()
    p2 = w.getMouse()

    #for now, assume that the user input a square area correctly
    llx = p1.getX()
    lly = p1.getY()
    urx = p2.getX()
    ury = p2.getY()

    #end while

    Nick J Chackowsky, Jul 9, 2004
    1. Advertisements

  2. Jeff Epler

    Jeff Epler Guest

    IMO you won't render mandelbrots at an acceptable speed using Python.

    Psyco might help, but probably not (it's unlikely to support complex

    You might speed things up a bit by doing something clever in numarray,
    effectively doing an iteration of the whole window worth of points in
    each loop, avoiding lots of interpreter overhead.

    But to get it competitive with a native code mandelbrot calculator,
    well, you'll probably have to write something that compiles to native

    You could also wait a few years, computers should be faster by then...


    Version: GnuPG v1.2.4 (GNU/Linux)

    -----END PGP SIGNATURE-----
    Jeff Epler, Jul 9, 2004
    1. Advertisements

  3. Jeff Epler

    Jeff Epler Guest

    Here's a program which calculates a similar result to yours using
    numarray. It calculates a 50x50 grid to 500 iterations in 3 seconds on
    a 750MHz machine. I didn't run yours so I don't know how this compares.

    Hm, says that your function runs in 2 ms per point with DEPTH=500
    given the point 0+0j, and of course runs faster on points that escape.
    Your program may run faster than the numarray implementation, or it
    might be a wash. (2ms * 50 * 50 == 5 seconds)

    This isn't a natural application for numarray.

    from numarray import *

    DEPTH = 500

    def coords(x0, y0, x1, y1, w, h):
    rx = arange(w).astype(Float32) * (x1-x0) / (w-1) + x0
    ry = arange(w).astype(Float32) * (y1-y0) / (h-1) + y0
    return add.outer(rx * 1j, ry)

    def f(z):
    C = z.copy()
    d = zeros(z.shape)

    for c in range(DEPTH):
    z = z*z - C
    zz = abs(z) > 3
    #print zz; print
    d += where(zz, 1, 0)
    if not (c % 8): # keep contents of z from overflowing
    z = where(zz, 10, z)
    return d

    # 20x20 prints nicely on the screen in numbers
    c = coords(-1.5, -1.5, 1.5, 1.5, 20, 20)
    print f(c)/10

    Version: GnuPG v1.2.4 (GNU/Linux)

    -----END PGP SIGNATURE-----
    Jeff Epler, Jul 9, 2004
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris

    Need Help speeding up mandelbrot algo

    Chris, Oct 30, 2003, in forum: C Programming
    Barry Schwarz
    Oct 31, 2003

Share This Page