Links

I have always enjoyed fractals, and was curious if scripting languages were up to the task. I wrote a very simple Mandelbrot set generator for my test. Rather than optimizing for each language, I tried to write each program in approximately the same way in each language to make a reasonable performance comparison.

Here are the results from running on my 867 mhz Powerbook G4. Shorter is better. Please note, the following benchmarks are not scientific, and were simply done to satisfy my curiosity. Your mileage may vary.

Feel free to send me ports to any other languages. The program should print the time in seconds that elapsed at the bottom in the form of 'Elapsed %0.2f'. If you can, include instructions for building on MacOS X.

2007-06-04 — I added a compiled OCAml version. Faster than both C and SBCL!

2007-06-02 — I updated the C version to utilize doubles instead of floats. Most scripting languages use doubles behind-the-scenes so this results in a more accurate comparision.

I updated Python to 2.5.1. Much faster!

I also began including speed relative to the fastest language.

2007-06-02 — I fixed up the Java, Ruby, Lua, Io, PHP, and Applescript versions. They were calculating one extra row and column (less than or equal to 39 rather than just less than). It turns out this didn't affect performance that much because those columns are over a low-iteration area.

I also starting running each language four times and averaging the time elapsed in order to make the results more consistent.

2007-05-28 — I added a SBCL Lisp version. Unfortunately, SBCL triggers a crash report on MacOS X every time I invoke it. SBCL's actual performance may vary slightly from the results shown.

2007-05-27 — I added a Ghostscript Postscript version.

2007-05-26 — I added an OCaml and Javascript (Using Mozilla's SpiderMonkey 1.6). I also removed the 'slow' TCL version and left the optimized one. After adding curly brackets around expressions, it increased the speed by 25 times.

2007-04-26 — I added an Applescript version.

2007-04-24 — Just for fun, I'm including optimized versions that don't change the character of the code (e.g. no loop unrolling). I received both Perl and TCL optimized versions and have included them in the benchmark. It's interesting to see how minor changes can drastically improve the performance of these scripts. If you notice something glaringly inefficient in one of the examples below, let me know.

2007-04-03 — I temporarily removed the Haskell version while I learn how to get it to print 'Elapsed <seconds>'. Anders Bergh ported the scripts on this page to D, C#. When I am able to run them from the command line, I'll add them to this page. Also pending are a Javascript, and a CL version.

Applescript

(* Applescript Version by Erik Wrenholt
I couldn't figure out how to write to stdout
so it buffers the output until the end. *)
on unixTime()
(do shell script "date +%s") as integer
end unixTime
on iterate(x, y)
set BAILOUT to 16
set MAX_ITERATIONS to 1000
set cr to y - 0.5
set ci to x
set zi to 0.0
set zr to 0.0
set i to 0
repeat while i < MAX_ITERATIONS
set i to i + 1
set temp to zr * zi
set zr2 to zr * zr
set zi2 to zi * zi
set zr to zr2 - zi2 + cr
set zi to temp + temp + ci
if zi2 + zr2 > BAILOUT then
return i
end if
end repeat
return 0
end iterate
set t to unixTime()
set mandelbrotString to ""
set nl to (ASCII character 10)
repeat with y from -39 to 38 by 1
set mandelbrotString to mandelbrotString & nl
repeat with x from -39 to 38 by 1
if iterate(x / 40.0, y / 40.0) = 0 then
set mandelbrotString to mandelbrotString & "*"
else
set mandelbrotString to mandelbrotString & " "
end if
end repeat
end repeat
set elapsed to unixTime() - t
mandelbrotString & nl & "Time Elapsed " & elapsed