I've 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.
— Erik Wrenholt (erik -at- timestretch.com) 2005-09-20
Language | Time | Relative Speed | |
---|---|---|---|
C gcc-4.0.1 | 0.05 seconds | 1.00 x | |
ocaml compiled 3.09.2 | 0.05 seconds | 1.00 x | |
SBCL 1.0.2 | 0.13 seconds | 2.55 x | |
Java 1.4.2 | 0.40 seconds | 8.00 x | |
Io 20070410 Vector | 1.40 seconds | 28.09 x | |
Lua 5.1 | 1.50 seconds | 30.00 x | |
ocaml bytecode 3.09.2 | 3.76 seconds | 75.15 x | |
Python 2.5.1 | 9.99 seconds | 199.80 x | |
Ghostscript 8.51 | 11.66 seconds | 233.12 x | |
Perl 5.8.6 Optimized | 12.37 seconds | 247.34 x | |
TCL 8.4 Optimized | 16.00 seconds | 320.00 x | |
Perl 5.8.6 | 21.75 seconds | 435.00 x | |
PHP 5.1.4 | 23.12 seconds | 462.40 x | |
Javascript SpiderMonkey v1.6 | 31.06 seconds | 621.27 x | |
Ruby 1.8.4 | 34.31 seconds | 686.18 x | |
Emacs Lisp | 47.25 seconds | 945.00 x | |
Applescript | 71.75 seconds | 1435.00 x | |
Io 20070410 | 85.26 seconds | 1705.13 x |
2007-06-08 — I enabled optimization in C. 'gcc -O3 Mandelbrot.c -o Mandelbrot.'
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.