C,Ruby, Io, PHP, Python, Lua, Java, Perl, Applescript, TCL, ELisp, Javascript, OCaml, Ghostscript性能比较

后端开发   发布日期:2025年06月23日   浏览次数:115

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.

Benchmark Generated on 2007-06-08 21:11:16

C gcc-4.0.1

  1. // by Erik Wrenholt
  2. #include <stdio.h>
  3. #include <sys/time.h>
  4. #define BAILOUT 16
  5. #define MAX_ITERATIONS 1000
  6. int mandelbrot(double x, double y)
  7. {
  8. double cr = y - 0.5;
  9. double ci = x;
  10. double zi = 0.0;
  11. double zr = 0.0;
  12. int i = 0;
  13. while(1) {
  14. i ++;
  15. double temp = zr * zi;
  16. double zr2 = zr * zr;
  17. double zi2 = zi * zi;
  18. zr = zr2 - zi2 + cr;
  19. zi = temp + temp + ci;
  20. if (zi2 + zr2 > BAILOUT)
  21. return i;
  22. if (i > MAX_ITERATIONS)
  23. return 0;
  24. }
  25. }
  26. int main (int argc, const char * argv[]) {
  27. struct timeval aTv;
  28. gettimeofday(&aTv, NULL);
  29. long init_time = aTv.tv_sec;
  30. long init_usec = aTv.tv_usec;
  31. int x,y;
  32. for (y = -39; y < 39; y++) {
  33. printf("/n");
  34. for (x = -39; x < 39; x++) {
  35. int i = mandelbrot(x/40.0, y/40.0);
  36. if (i==0)
  37. printf("*");
  38. else
  39. printf(" ");
  40. }
  41. }
  42. printf ("/n");
  43. gettimeofday(&aTv,NULL);
  44. double query_time = (aTv.tv_sec - init_time) + (double)(aTv.tv_usec - init_usec)/1000000.0;
  45. printf ("C Elapsed %0.2f/n", query_time);
  46. return 0;
  47. }

Java 1.4.2

  1. // by Erik Wrenholt
  2. import java.util.*;
  3. class Mandelbrot
  4. {
  5. static int BAILOUT = 16;
  6. static int MAX_ITERATIONS = 1000;
  7. private static int iterate(float x, float y)
  8. {
  9. float cr = y-0.5f;
  10. float ci = x;
  11. float zi = 0.0f;
  12. float zr = 0.0f;
  13. int i = 0;
  14. while (true) {
  15. i++;
  16. float temp = zr * zi;
  17. float zr2 = zr * zr;
  18. float zi2 = zi * zi;
  19. zr = zr2 - zi2 + cr;
  20. zi = temp + temp + ci;
  21. if (zi2 + zr2 > BAILOUT)
  22. return i;
  23. if (i > MAX_ITERATIONS)
  24. return 0;
  25. }
  26. }
  27. public static void main(String args[])
  28. {
  29. Date d1 = new Date();
  30. int x,y;
  31. for (y = -39; y < 39; y++) {
  32. System.out.print("/n");
  33. for (x = -39; x < 39; x++) {
  34. if (iterate(x/40.0f,y/40.0f) == 0)
  35. System.out.print("*");
  36. else
  37. System.out.print(" ");
  38. }
  39. }
  40. Date d2 = new Date();
  41. long diff = d2.getTime() - d1.getTime();
  42. System.out.println("/nJava Elapsed " + diff/1000.0f);
  43. }
  44. }

Io 20070410 Vector

  1. #!/usr/local/bin/io
  2. # Vectorized by Steve Dekorte
  3. printSet := method(
  4. bailout := 16
  5. max_iterations := 1000
  6. cr := Vector clone
  7. ci := Vector clone
  8. i := 0
  9. for(y, -39, 38,
  10. for(x, -39, 38,
  11. cr atPut(i, y/40.0 - 0.5)
  12. ci atPut(i, (x/40.0))
  13. i = i + 1
  14. )
  15. )
  16. size := cr size
  17. zi := Vector clone setSize(size)
  18. zr := Vector clone setSize(size)
  19. zr2 := Vector clone setSize(size)
  20. zi2 := Vector clone setSize(size)
  21. temp := Vector clone setSize(size)
  22. for(i, 1, max_iterations,
  23. temp copy(zr) *= zi
  24. zr2 copy(zr) square
  25. zi2 copy(zi) square
  26. zr copy(zr2) -= zi2
  27. zr += cr
  28. zi copy(temp) *= 2
  29. zi += ci
  30. )
  31. result := zi2 + zr2
  32. i := 0
  33. for(y, -39, 38,
  34. writeln
  35. for(x, -39, 38,
  36. r := result at(i)
  37. write(if( r > 0, "*", " "))
  38. i = i + 1
  39. )
  40. )
  41. )
  42. writeln("/nIo Elapsed " .. Date secondsToRun(printSet))

Lua 5.1

  1. #!/usr/local/bin/lua
  2. -- By Erik Wrenholt
  3. local BAILOUT = 16
  4. local MAX_ITERATIONS = 1000
  5. function iterate(x,y)
  6. local cr = y-0.5
  7. local ci = x
  8. local zi = 0.0
  9. local zr = 0.0
  10. local i = 0
  11. while 1 do
  12. i = i+1
  13. local temp = zr * zi
  14. local zr2 = zr*zr
  15. local zi2 = zi*zi
  16. zr = zr2-zi2+cr
  17. zi = temp+temp+ci
  18. if (zi2+zr2 > BAILOUT) then
  19. return i
  20. end
  21. if (i > MAX_ITERATIONS) then
  22. return 0
  23. end
  24. end
  25. end
  26. function mandelbrot()
  27. local t = os.time()
  28. for y = -39, 38 do
  29. for x = -39, 38 do
  30. if (iterate(x/40.0, y/40) == 0) then
  31. io.write("*")
  32. else
  33. io.write(" ")
  34. end
  35. end
  36. io.write("/n")
  37. end
  38. io.write(string.format("Time Elapsed %d/n", os.time() - t))
  39. end
  40. mandelbrot()

ocaml compiled 3.09.2

  1. (* Courtesy of pango on #ocaml *)
  2. let bailout = 16.
  3. let max_iterations = 1000
  4. let mandelbrot x y =
  5. let cr = y -. 0.5 in
  6. let ci = x in
  7. let zi = ref 0.0 in
  8. let zr = ref 0.0 in
  9. let i = ref 0 in
  10. let continue = ref true in
  11. let result = ref 0 in
  12. while !continue do
  13. incr i;
  14. let temp = !zr *. !zi in
  15. let zr2 = !zr *. !zr in
  16. let zi2 = !zi *. !zi in
  17. zr := zr2 -. zi2 +. cr;
  18. zi := temp +. temp +. ci;
  19. if zi2 +. zr2 > bailout then begin
  20. result := !i;
  21. continue := false;
  22. end
  23. else if !i > max_iterations then
  24. continue := false;
  25. done;
  26. !result
  27. let () =
  28. let start_time = Unix.gettimeofday () in
  29. for y = -39 to 38 do
  30. print_newline ();
  31. for x = -39 to 38 do
  32. let i = mandelbrot (float x /. 40.) (float y /. 40.) in
  33. print_char (if i = 0 then '*' else ' ');
  34. done
  35. done;
  36. print_newline ();
  37. let stop_time = Unix.gettimeofday () in
  38. let query_time = stop_time -. start_time in
  39. Printf.printf "OCaml Elapsed %0.2f/n" query_time

ocaml bytecode 3.09.2

  1. #!/opt/local/bin/ocamlrun ocaml unix.cma
  2. (* Courtesy of pango on #ocaml *)
  3. let bailout = 16.
  4. let max_iterations = 1000
  5. let mandelbrot x y =
  6. let cr = y -. 0.5 in
  7. let ci = x in
  8. let zi = ref 0.0 in
  9. let zr = ref 0.0 in
  10. let i = ref 0 in
  11. let continue = ref true in
  12. let result = ref 0 in
  13. while !continue do
  14. incr i;
  15. let temp = !zr *. !zi in
  16. let zr2 = !zr *. !zr in
  17. let zi2 = !zi *. !zi in
  18. zr := zr2 -. zi2 +. cr;
  19. zi := temp +. temp +. ci;
  20. if zi2 +. zr2 > bailout then begin
  21. result := !i;
  22. continue := false;
  23. end
  24. else if !i > max_iterations then
  25. continue := false;
  26. done;
  27. !result
  28. let () =
  29. let start_time = Unix.gettimeofday () in
  30. for y = -39 to 38 do
  31. print_newline ();
  32. for x = -39 to 38 do
  33. let i = mandelbrot (float x /. 40.) (float y /. 40.) in
  34. print_char (if i = 0 then '*' else ' ');
  35. done
  36. done;
  37. print_newline ();
  38. let stop_time = Unix.gettimeofday () in
  39. let query_time = stop_time -. start_time in
  40. Printf.printf "OCaml Elapsed %0.2f/n" query_time

Python 2.5.1

  1. #!/usr/local/bin/python
  2. # by Daniel Rosengren
  3. import sys, time
  4. stdout = sys.stdout
  5. BAILOUT = 16
  6. MAX_ITERATIONS = 1000
  7. class Iterator:
  8. def __init__(self):
  9. print 'Rendering...'
  10. for y in range(-39, 39):
  11. stdout.write('/n')
  12. for x in range(-39, 39):
  13. i = self.mandelbrot(x/40.0, y/40.0)
  14. if i == 0:
  15. stdout.write('*')
  16. else:
  17. stdout.write(' ')
  18. def mandelbrot(self, x, y):
  19. cr = y - 0.5
  20. ci = x
  21. zi = 0.0
  22. zr = 0.0
  23. i = 0
  24. while True:
  25. i += 1
  26. temp = zr * zi
  27. zr2 = zr * zr
  28. zi2 = zi * zi
  29. zr = zr2 - zi2 + cr
  30. zi = temp + temp + ci
  31. if zi2 + zr2 > BAILOUT:
  32. return i
  33. if i > MAX_ITERATIONS:
  34. return 0
  35. t = time.time()
  36. Iterator()
  37. print '/nPython Elapsed %.02f' % (time.time() - t)

Ghostscript 8.51

  1. %!PS
  2. %% frac.ps by Jeff Zaroyko
  3. %% a transliteration of the program by Erik Wrenholt
  4. %% at http://www.timestretch.com/FractalBenchmark.html
  5. /star {(*) print } bind def
  6. /space { ( ) print } bind def
  7. realtime pop
  8. /BAILOUT 16 def
  9. /MAX_ITERATIONS 1000 def
  10. /mandelbrot { %% mx my
  11. /mx exch def
  12. /my exch def
  13. /cr my 0.5 sub def
  14. /ci mx def
  15. /zi 0.0 def
  16. /zr 0.0 def
  17. /i 0 def
  18. {
  19. /i i 1 add def
  20. /temp zr zi mul def
  21. /zr2 zr zr mul def
  22. /zi2 zi zi mul def
  23. /zr zr2 zi2 sub cr add def
  24. /zi temp temp add ci add def
  25. zi2 zr2 add BAILOUT gt
  26. {
  27. i exit
  28. } if
  29. i MAX_ITERATIONS gt
  30. {
  31. 0 exit
  32. } if
  33. } loop
  34. } bind def
  35. % main
  36. /y -39 def
  37. {
  38. y 39 lt
  39. {
  40. /y y 1 add def
  41. (/n) print
  42. /x -39 def
  43. {
  44. x 39 lt
  45. {
  46. /x x 1 add def
  47. y 40.0 div x 40.0 div mandelbrot
  48. 0 eq { star } { space } ifelse
  49. } { exit } ifelse
  50. } loop
  51. } { exit }ifelse % Done.
  52. } loop
  53. realtime dup log ceiling cvi string cvs
  54. (/nTotal time Elapsed ) print print (ms/n) print
  55. quit

Perl 5.8.6 Optimized

  1. #!/usr/bin/perl
  2. # Ported from C to Perl by Anders Bergh <anders1@gmail.com>
  3. # Some Perlification by John Gabriele (4-24-2007).
  4. use strict;
  5. use warnings;
  6. use Time::HiRes qw( gettimeofday );
  7. my $BAILOUT = 16;
  8. my $MAX_ITERATIONS = 1000;
  9. my $begin = gettimeofday();
  10. sub mandelbrot {
  11. my ( $x, $y ) = @_;
  12. my $cr = $y - 0.5;
  13. my $ci = $x;
  14. my ( $zi, $zr ) = ( 0.0, 0.0 );
  15. my $i = 0;
  16. my ( $temp, $zr2, $zi2 );
  17. while ( 1 ) {
  18. $i += 1;
  19. $temp = $zr * $zi;
  20. $zr2 = $zr * $zr;
  21. $zi2 = $zi * $zi;
  22. $zr = $zr2 - $zi2 + $cr;
  23. $zi = $temp + $temp + $ci;
  24. if ( $zi2 + $zr2 > $BAILOUT ) {
  25. return $i;
  26. }
  27. if ( $i > $MAX_ITERATIONS ) {
  28. return 0;
  29. }
  30. }
  31. }
  32. for ( my $y = -39; $y < 39; $y++ ) {
  33. print( "/n" );
  34. my $i;
  35. for ( my $x = -39; $x < 39; $x++ ) {
  36. $i = mandelbrot( $x / 40.0, $y / 40.0 );
  37. if ( $i == 0 ) {
  38. print q{*};
  39. }
  40. else {
  41. print q{ };
  42. }
  43. }
  44. }
  45. print "/n";
  46. my $end = gettimeofday() - $begin;
  47. print "Perl Elapsed $end/n";

Perl 5.8.6

  1. #!/usr/bin/perl
  2. # Ported from C to Perl by Anders Bergh <anders1@gmail.com>
  3. #
  4. $BAILOUT=16;
  5. $MAX_ITERATIONS=1000;
  6. $begin = time();
  7. sub mandelbrot {
  8. local $x = $_[0];
  9. local $y = $_[1];
  10. local $cr = $y - 0.5;
  11. local $ci = $x;
  12. local $zi = 0.0;
  13. local $zr = 0.0;
  14. local $i = 0;
  15. while (1)
  16. {
  17. $i = $i + 1;
  18. local $temp = $zr * $zi;
  19. local $zr2 = $zr * $zr;
  20. local $zi2 = $zi * $zi;
  21. $zr = $zr2 - $zi2 + $cr;
  22. $zi = $temp + $temp + $ci;
  23. if ($zi2 + $zr2 > $BAILOUT)
  24. {
  25. return $i;
  26. }
  27. if ($i > $MAX_ITERATIONS)
  28. {
  29. return 0;
  30. }
  31. }
  32. }
  33. for ($y = -39; $y < 39; $y++)
  34. {
  35. print("/n");
  36. for ($x = -39; $x < 39; $x++)
  37. {
  38. $i = mandelbrot($x/40.0, $y/40.0);
  39. if ($i == 0)
  40. {
  41. print("*");
  42. }
  43. else
  44. {
  45. print(" ");
  46. }
  47. }
  48. }
  49. print("/n");
  50. $end = time() - $begin;
  51. print "Perl Elapsed $end/n";

PHP 5.1.4

  1. #!/usr/local/php5/bin/php
  2. <?php
  3. define("BAILOUT",16);
  4. define("MAX_ITERATIONS",1000);
  5. class Mandelbrot
  6. {
  7. function Mandelbrot()
  8. {
  9. $d1 = microtime(1);
  10. for ($y = -39; $y < 39; $y++) {
  11. echo("/n");
  12. for ($x = -39; $x < 39; $x++) {
  13. if ($this->iterate($x/40.0,$y/40.0) == 0)
  14. echo("*");
  15. else
  16. echo(" ");
  17. }
  18. }
  19. $d2 = microtime(1);
  20. $diff = $d2 - $d1;
  21. printf("/nPHP Elapsed %0.2f", $diff);
  22. }
  23. function iterate($x,$y)
  24. {
  25. $cr = $y-0.5;
  26. $ci = $x;
  27. $zi = 0.0;
  28. $zr = 0.0;
  29. $i = 0;
  30. while (true) {
  31. $i++;
  32. $temp = $zr * $zi;
  33. $zr2 = $zr * $zr;
  34. $zi2 = $zi * $zi;
  35. $zr = $zr2 - $zi2 + $cr;
  36. $zi = $temp + $temp + $ci;
  37. if ($zi2 + $zr2 > BAILOUT)
  38. return $i;
  39. if ($i > MAX_ITERATIONS)
  40. return 0;
  41. }
  42. }
  43. }
  44. $m = new Mandelbrot();
  45. ?>

Ruby 1.8.4

  1. #!/usr/local/bin/ruby
  2. BAILOUT = 16
  3. MAX_ITERATIONS = 1000
  4. class Mandelbrot
  5. def initialize
  6. puts "Rendering"
  7. for y in -39...39 do
  8. puts
  9. for x in -39...39 do
  10. i = iterate(x/40.0,y/40.0)
  11. if (i == 0)
  12. print "*"
  13. else
  14. print " "
  15. end
  16. end
  17. end
  18. end
  19. def iterate(x,y)
  20. cr = y-0.5
  21. ci = x
  22. zi = 0.0
  23. zr = 0.0
  24. i = 0
  25. while(1)
  26. i += 1
  27. temp = zr * zi
  28. zr2 = zr * zr
  29. zi2 = zi * zi
  30. zr = zr2 - zi2 + cr
  31. zi = temp + temp + ci
  32. return i if (zi2 + zr2 > BAILOUT)
  33. return 0 if (i > MAX_ITERATIONS)
  34. end
  35. end
  36. end
  37. time = Time.now
  38. Mandelbrot.new
  39. puts
  40. puts "Ruby Elapsed %f" % (Time.now - time)

Javascript SpiderMonkey v1.6

  1. #!/usr/local/bin/js
  2. /* Javascript version by Patrick Haller.*/
  3. function mandelbrot(x, y) {
  4. var cr = y - 0.5;
  5. var ci = x;
  6. var zi = 0.0;
  7. var zr = 0.0;
  8. var i = 0;
  9. var BAILOUT = 16;
  10. var MAX_ITERATIONS = 1000;
  11. while(1) {
  12. i++;
  13. var temp = zr * zi;
  14. var zr2 = zr * zr;
  15. var zi2 = zi * zi;
  16. zr = zr2 - zi2 + cr;
  17. zi = temp + temp + ci;
  18. if (zi2 + zr2 > BAILOUT) {
  19. return i;
  20. }
  21. if (i > MAX_ITERATIONS) {
  22. return 0;
  23. }
  24. }
  25. }
  26. function mandelbrot_run() {
  27. var x; var y;
  28. output = "";
  29. var date = new Date();
  30. for (y = -39; y < 39; y++) {
  31. print(output);
  32. output = "";
  33. for (x = -39; x < 39; x++) {
  34. var i = mandelbrot(x/40.0, y/40.0);
  35. if (i==0) {
  36. output += "*";
  37. }
  38. else {
  39. output += " ";
  40. }
  41. }
  42. }
  43. var date2 = new Date();
  44. output += "/nJavaScript Elapsed " + (date2.getTime() - date.getTime()) / 1000;
  45. print(output);
  46. return false;
  47. }
  48. mandelbrot_run();

TCL 8.4 Optimized

  1. #!/usr/bin/tclsh
  2. # Optimized Version by Samuel Zafrany
  3. # Ported from C by Anders Bergh <anders1@gmail.com>
  4. package require Tcl 8.4
  5. set BAILOUT 16
  6. set MAX_ITERATIONS 1000
  7. proc mandelbrot {x y} {
  8. global BAILOUT
  9. global MAX_ITERATIONS
  10. set cr [expr {$y - 0.5}]
  11. set ci $x
  12. set zi 0.0
  13. set zr 0.0
  14. set i 0
  15. while {1} {
  16. incr i
  17. set temp [expr {$zr * $zi}]
  18. set zr2 [expr {$zr * $zr}]
  19. set zi2 [expr {$zi * $zi}]
  20. set zr [expr {$zr2 - $zi2 + $cr}]
  21. set zi [expr {$temp + $temp + $ci}]
  22. if {$zi2 + $zr2 > $BAILOUT} {
  23. return $i
  24. }
  25. if {$i > $MAX_ITERATIONS} {
  26. return 0
  27. }
  28. }
  29. }
  30. set begin [clock clicks -milliseconds]
  31. for {set y -39} {$y < 39} {incr y} {
  32. puts ""
  33. for {set x -39} {$x < 39} {incr x} {
  34. set i [mandelbrot [expr {$x / 40.0}] [expr {$y / 40.0}]]
  35. if {$i == 0} {
  36. puts -nonewline "*"
  37. } else {
  38. puts -nonewline " "
  39. }
  40. flush stdout
  41. }
  42. }
  43. puts ""
  44. set diff [expr [clock clicks -milliseconds] - $begin]
  45. #puts "Tcl Elapsed [expr int($diff / 1000)] seconds ($diff ms)"
  46. puts "Tcl Elapsed [expr int($diff / 1000)]"

Emacs Lisp

  1. ; By Erik Wrenholt
  2. ; emacs -l Mandelbrot.lisp -batch
  3. (defun iterate (xx yy)
  4. (let*
  5. (
  6. (BAILOUT 16.0)
  7. (MAX_ITERATIONS 1000)
  8. (bail_val 0)
  9. (cr (- yy 0.5))
  10. (ci xx)
  11. (zi 0.0)
  12. (zr 0.0)
  13. (i 0)
  14. )
  15. (while (and (< i MAX_ITERATIONS) (< bail_val BAILOUT))
  16. (setq i (+ 1 i))
  17. (setq temp (* zr zi))
  18. (setq zr2 (* zr zr))
  19. (setq zi2 (* zi zi))
  20. (setq zr (+ (- zr2 zi2) cr))
  21. (setq zi (+ temp temp ci))
  22. (setq bail_val (+ zi2 zr2)))
  23. i)
  24. )
  25. (defun mandelbrot()
  26. (setq yy -39)
  27. (while (< yy 39)
  28. (setq yy (+ 1 yy))
  29. (setq xx -39)
  30. (while (< xx 39)
  31. (setq xx (+ 1 xx))
  32. (if (= (iterate (/ xx 40.0) (/ yy 40.0)) 1000)
  33. (princ "*")
  34. (princ " ")
  35. )
  36. )
  37. (princ "/n")
  38. ))
  39. (setq the-time (cadr (current-time)))
  40. (mandelbrot)
  41. (princ (format "Elapsed %d" (- (cadr (current-time)) the-time)))

Applescript

  1. (* Applescript Version by Erik Wrenholt
  2. I couldn't figure out how to write to stdout
  3. so it buffers the output until the end. *)
  4. on unixTime()
  5. (do shell script "date +%s") as integer
  6. end unixTime
  7. on iterate(x, y)
  8. set BAILOUT to 16
  9. set MAX_ITERATIONS to 1000
  10. set cr to y - 0.5
  11. set ci to x
  12. set zi to 0.0
  13. set zr to 0.0
  14. set i to 0
  15. repeat while i < MAX_ITERATIONS
  16. set i to i + 1
  17. set temp to zr * zi
  18. set zr2 to zr * zr
  19. set zi2 to zi * zi
  20. set zr to zr2 - zi2 + cr
  21. set zi to temp + temp + ci
  22. if zi2 + zr2 > BAILOUT then
  23. return i
  24. end if
  25. end repeat
  26. return 0
  27. end iterate
  28. set t to unixTime()
  29. set mandelbrotString to ""
  30. set nl to (ASCII character 10)
  31. repeat with y from -39 to 38 by 1
  32. set mandelbrotString to mandelbrotString & nl
  33. repeat with x from -39 to 38 by 1
  34. if iterate(x / 40.0, y / 40.0) = 0 then
  35. set mandelbrotString to mandelbrotString & "*"
  36. else
  37. set mandelbrotString to mandelbrotString & " "
  38. end if
  39. end repeat
  40. end repeat
  41. set elapsed to unixTime() - t
  42. mandelbrotString & nl & "Time Elapsed " & elapsed

Io 20070410

  1. #!/usr/local/bin/IoServer
  2. # by Erik Wrenholt
  3. iterator := Object clone do (
  4. bailout := 16
  5. max_iterations := 1000
  6. mandelbrot := method (x,y,
  7. cr := y - 0.5
  8. ci := x
  9. zi := 0.0
  10. zr := 0.0
  11. i := 0
  12. while (1,
  13. i = i + 1
  14. temp := zr * zi
  15. zr2 := zr * zr
  16. zi2 := zi * zi
  17. zr := zr2 - zi2 + cr
  18. zi := temp + temp +ci
  19. if (zi2 + zr2 > bailout,
  20. return i)
  21. if (i > max_iterations,
  22. return 0)
  23. )
  24. )
  25. print_set := method (
  26. writeln("Rendering...")
  27. for(y, -39, 38,
  28. write("/n")
  29. for(x, -39, 38,
  30. i := mandelbrot(x/40.0,y/40.0)
  31. if (i == 0, write("*"),write(" "))
  32. )
  33. )
  34. )
  35. )
  36. writeln
  37. writeln ("Io Elapsed " .. Date secondsToRun(iterator print_set))

SBCL 1.0.2

  1. ; sbcl lisp version by mandeep singh
  2. (declaim (optimize (speed 3)))
  3. (defconstant +BAILOUT+ 16)
  4. (defconstant +MAX-ITERATIONS+ 1000)
  5. (defun mandelbrot (x y)
  6. (declare (type single-float x y))
  7. (let ((cr (- y 0.5))
  8. (ci x)
  9. (zi 0.0)
  10. (zr 0.0))
  11. (declare (type single-float cr ci zi zr))
  12. (do ((i 0 (incf i)))
  13. (nil)
  14. (let* ((temp (the single-float (* zr zi)))
  15. (zr2 (the single-float (* zr zr)))
  16. (zi2 (the single-float (* zi zi))))
  17. (declare (type single-float temp zr2 zi2)
  18. (type fixnum i))
  19. (setq zr (the single-float (+ (- zr2 zi2) cr)))
  20. (setq zi (the single-float (+ temp temp ci)))
  21. (if (> (the single-float (+ zi2 zr2)) +BAILOUT+)
  22. (return-from mandelbrot i))
  23. (if (> i +MAX-ITERATIONS+)
  24. (return-from mandelbrot 0))))))
  25. (defun main ()
  26. (let ((tstart)
  27. (tfinish))
  28. (setq tstart (get-internal-real-time))
  29. (do ((y -39 (incf y)))
  30. ((= (the fixnum y) 39))
  31. (format t "~%")
  32. (do ((x -39 (incf x)))
  33. ((= (the fixnum x) 39))
  34. (let ((i (mandelbrot (the single-float (/ x 40.0))
  35. (the single-float (/ y 40.0)))))
  36. (declare (type fixnum i x y))
  37. (if (zerop i)
  38. (format t "*")
  39. (format t " ")))))
  40. (format t "~%")
  41. (setq tfinish (get-internal-real-time))
  42. (format t "SBCL Elapsed ~,2F~%" (coerce (/ (- tfinish tstart) internal-time-units-per-second) 'float))))
  43. (progn
  44. (main)
  45. (quit))

以上就是C,Ruby, Io, PHP, Python, Lua, Java, Perl, Applescript, TCL, ELisp, Javascript, OCaml, Ghostscript性能比较的详细内容,更多关于C,Ruby, Io, PHP, Python, Lua, Java, Perl, Applescript, TCL, ELisp, Javascript, OCaml, Ghostscript性能比较的资料请关注九品源码其它相关文章!