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

来源:互联网 发布:linux修改用户目录 编辑:程序博客网 时间:2024/04/30 12:15

Ruby, Io, PHP, Python, Lua, Java, Perl, Applescript, TCL, ELisp, Javascript, OCaml, Ghostscript, and C Fractal Benchmark

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

LanguageTime Relative SpeedC gcc-4.0.10.05 seconds 1.00 xocaml compiled 3.09.20.05 seconds 1.00 xSBCL 1.0.20.13 seconds 2.55 xJava 1.4.20.40 seconds 8.00 xIo 20070410 Vector1.40 seconds 28.09 xLua 5.11.50 seconds 30.00 xocaml bytecode 3.09.23.76 seconds 75.15 xPython 2.5.19.99 seconds 199.80 xGhostscript 8.5111.66 seconds 233.12 xPerl 5.8.6 Optimized12.37 seconds 247.34 xTCL 8.4 Optimized16.00 seconds 320.00 xPerl 5.8.621.75 seconds 435.00 xPHP 5.1.423.12 seconds 462.40 xJavascript SpiderMonkey v1.631.06 seconds 621.27 xRuby 1.8.434.31 seconds 686.18 xEmacs Lisp47.25 seconds 945.00 xApplescript71.75 seconds 1435.00 xIo 2007041085.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

// by Erik Wrenholt#include <stdio.h>#include <sys/time.h>#define BAILOUT 16#define MAX_ITERATIONS 1000int mandelbrot(double x, double y){double cr = y - 0.5;double ci = x;double zi = 0.0;double zr = 0.0;int i = 0;while(1) {i ++;double temp = zr * zi;double zr2 = zr * zr;double zi2 = zi * zi;zr = zr2 - zi2 + cr;zi = temp + temp + ci;if (zi2 + zr2 > BAILOUT)return i;if (i > MAX_ITERATIONS)return 0;}}int main (int argc, const char * argv[]) {struct timeval aTv;gettimeofday(&aTv, NULL);long init_time = aTv.tv_sec;long init_usec = aTv.tv_usec;int x,y;for (y = -39; y < 39; y++) {printf("/n");for (x = -39; x < 39; x++) {int i = mandelbrot(x/40.0, y/40.0);if (i==0)printf("*");elseprintf(" ");}}printf ("/n");gettimeofday(&aTv,NULL);double query_time = (aTv.tv_sec - init_time) + (double)(aTv.tv_usec - init_usec)/1000000.0;printf ("C Elapsed %0.2f/n", query_time);    return 0;}

Java 1.4.2

// by Erik Wrenholtimport java.util.*;class Mandelbrot{  static int BAILOUT = 16;static int MAX_ITERATIONS = 1000;private static int iterate(float x, float y){float cr = y-0.5f;float ci = x;float zi = 0.0f;float zr = 0.0f;int i = 0;while (true) {i++;float temp = zr * zi;float zr2 = zr * zr;float zi2 = zi * zi;zr = zr2 - zi2 + cr;zi = temp + temp + ci;if (zi2 + zr2 > BAILOUT)return i;if (i > MAX_ITERATIONS)return 0;}}public static void main(String args[]){Date d1 = new Date();int x,y;for (y = -39; y < 39; y++) {System.out.print("/n");for (x = -39; x < 39; x++) {if (iterate(x/40.0f,y/40.0f) == 0) System.out.print("*");elseSystem.out.print(" ");}}Date d2 = new Date();long diff = d2.getTime() - d1.getTime();System.out.println("/nJava Elapsed " + diff/1000.0f);}}

Io 20070410 Vector

#!/usr/local/bin/io# Vectorized by Steve DekorteprintSet := method(    bailout := 16    max_iterations := 1000    cr := Vector clone    ci := Vector clone    i := 0    for(y, -39, 38,for(x, -39, 38,    cr atPut(i, y/40.0 - 0.5)    ci atPut(i, (x/40.0))    i = i + 1)    )    size := cr size    zi := Vector clone setSize(size)    zr := Vector clone setSize(size)     zr2 := Vector clone setSize(size)    zi2 := Vector clone setSize(size)    temp := Vector clone setSize(size)    for(i, 1, max_iterations,temp copy(zr) *= zizr2 copy(zr) squarezi2 copy(zi) squarezr copy(zr2) -= zi2 zr += crzi copy(temp) *= 2zi += ci    )    result := zi2 + zr2    i := 0    for(y, -39, 38,writelnfor(x, -39, 38,    r := result at(i)     write(if( r > 0, "*", " "))    i = i + 1)    ))writeln("/nIo Elapsed " .. Date secondsToRun(printSet))

Lua 5.1

#!/usr/local/bin/lua-- By Erik Wrenholtlocal BAILOUT = 16local MAX_ITERATIONS = 1000function iterate(x,y)local cr = y-0.5local ci = xlocal zi = 0.0local zr = 0.0local i = 0while 1 doi = i+1local temp = zr * zilocal zr2 = zr*zrlocal zi2 = zi*zizr = zr2-zi2+crzi = temp+temp+ciif (zi2+zr2 > BAILOUT) thenreturn iendif (i > MAX_ITERATIONS) thenreturn 0endendendfunction mandelbrot()local t = os.time()for y = -39, 38 dofor x = -39, 38 doif (iterate(x/40.0, y/40) == 0) thenio.write("*")elseio.write(" ")endendio.write("/n")endio.write(string.format("Time Elapsed %d/n", os.time() - t))endmandelbrot()

ocaml compiled 3.09.2

(* Courtesy of pango on #ocaml *)let bailout = 16.let max_iterations = 1000let mandelbrot x y =  let cr = y -. 0.5 in  let ci = x in  let zi = ref 0.0 in  let zr = ref 0.0 in  let i = ref 0 in  let continue = ref true in  let result = ref 0 in  while !continue do    incr i;    let temp = !zr *. !zi in    let zr2 = !zr *. !zr in    let zi2 = !zi *. !zi in    zr := zr2 -. zi2 +. cr;    zi := temp +. temp +. ci;    if zi2 +. zr2 > bailout then begin      result := !i;      continue := false;    end    else if !i > max_iterations then      continue := false;  done;  !resultlet () =  let start_time = Unix.gettimeofday () in  for y = -39 to 38 do    print_newline ();    for x = -39 to 38 do      let i = mandelbrot (float x /. 40.) (float y /. 40.) in      print_char (if i = 0 then '*' else ' ');    done  done;  print_newline ();  let stop_time = Unix.gettimeofday () in  let query_time = stop_time -. start_time in  Printf.printf "OCaml Elapsed %0.2f/n" query_time

ocaml bytecode 3.09.2

#!/opt/local/bin/ocamlrun ocaml unix.cma(* Courtesy of pango on #ocaml *)let bailout = 16.let max_iterations = 1000let mandelbrot x y =  let cr = y -. 0.5 in  let ci = x in  let zi = ref 0.0 in  let zr = ref 0.0 in  let i = ref 0 in  let continue = ref true in  let result = ref 0 in  while !continue do    incr i;    let temp = !zr *. !zi in    let zr2 = !zr *. !zr in    let zi2 = !zi *. !zi in    zr := zr2 -. zi2 +. cr;    zi := temp +. temp +. ci;    if zi2 +. zr2 > bailout then begin      result := !i;      continue := false;    end    else if !i > max_iterations then      continue := false;  done;  !resultlet () =  let start_time = Unix.gettimeofday () in  for y = -39 to 38 do    print_newline ();    for x = -39 to 38 do      let i = mandelbrot (float x /. 40.) (float y /. 40.) in      print_char (if i = 0 then '*' else ' ');    done  done;  print_newline ();  let stop_time = Unix.gettimeofday () in  let query_time = stop_time -. start_time in  Printf.printf "OCaml Elapsed %0.2f/n" query_time

Python 2.5.1

#!/usr/local/bin/python# by Daniel Rosengrenimport sys, timestdout = sys.stdoutBAILOUT = 16MAX_ITERATIONS = 1000class Iterator:  def __init__(self):    print 'Rendering...'    for y in range(-39, 39):      stdout.write('/n')      for x in range(-39, 39):        i = self.mandelbrot(x/40.0, y/40.0)                if i == 0:          stdout.write('*')        else:          stdout.write(' ')      def mandelbrot(self, x, y):    cr = y - 0.5    ci = x    zi = 0.0    zr = 0.0    i = 0    while True:      i += 1      temp = zr * zi      zr2 = zr * zr      zi2 = zi * zi      zr = zr2 - zi2 + cr      zi = temp + temp + ci         if zi2 + zr2 > BAILOUT:        return i      if i > MAX_ITERATIONS:        return 0t = time.time()Iterator()print '/nPython Elapsed %.02f' % (time.time() - t)

Ghostscript 8.51

%!PS%% frac.ps by Jeff Zaroyko%% a transliteration of the program by Erik Wrenholt%% at http://www.timestretch.com/FractalBenchmark.html/star {(*) print } bind def/space { ( ) print } bind defrealtime pop/BAILOUT 16 def/MAX_ITERATIONS 1000 def/mandelbrot { %% mx my    /mx exch def    /my exch def    /cr my 0.5 sub def    /ci mx def    /zi 0.0 def    /zr 0.0 def    /i 0 def    {     /i i 1 add def     /temp zr zi mul def     /zr2 zr zr mul def     /zi2 zi zi mul def     /zr zr2 zi2 sub cr add def     /zi temp temp add ci add def     zi2 zr2 add BAILOUT gt {     i exit  } if     i MAX_ITERATIONS gt {     0 exit  } if    } loop} bind def% main/y -39 def{    y 39 lt    {/y y 1 add def(/n) print /x -39 def{    x 39 lt    {/x x 1 add defy 40.0 div x 40.0 div mandelbrot0 eq { star  } { space  } ifelse    } { exit } ifelse} loop    } { exit }ifelse  % Done.} looprealtime dup log ceiling cvi string cvs(/nTotal time Elapsed ) print print (ms/n) printquit

Perl 5.8.6 Optimized

#!/usr/bin/perl# Ported from C to Perl by Anders Bergh <anders1@gmail.com># Some Perlification by John Gabriele (4-24-2007).use strict;use warnings;use Time::HiRes qw( gettimeofday );my $BAILOUT = 16;my $MAX_ITERATIONS = 1000;my $begin = gettimeofday();sub mandelbrot {    my ( $x, $y ) = @_;    my $cr = $y - 0.5;    my $ci = $x;    my ( $zi, $zr ) = ( 0.0, 0.0 );    my $i = 0;    my ( $temp, $zr2, $zi2 );    while ( 1 ) {        $i += 1;        $temp = $zr * $zi;        $zr2  = $zr * $zr;        $zi2  = $zi * $zi;        $zr = $zr2 - $zi2 + $cr;        $zi = $temp + $temp + $ci;        if ( $zi2 + $zr2 > $BAILOUT ) {            return $i;        }        if ( $i > $MAX_ITERATIONS ) {            return 0;        }    }}for ( my $y = -39; $y < 39; $y++ ) {    print( "/n" );    my $i;    for ( my $x = -39; $x < 39; $x++ ) {        $i = mandelbrot( $x / 40.0, $y / 40.0 );        if ( $i == 0 ) {            print q{*};        }        else {            print q{ };        }    }}print "/n";my $end = gettimeofday() - $begin;print "Perl Elapsed $end/n";

Perl 5.8.6

#!/usr/bin/perl# Ported from C to Perl by Anders Bergh <anders1@gmail.com>#$BAILOUT=16;$MAX_ITERATIONS=1000;$begin = time();sub mandelbrot {       local $x = $_[0];       local $y = $_[1];       local $cr = $y - 0.5;       local $ci = $x;       local $zi = 0.0;       local $zr = 0.0;       local $i = 0;       while (1)       {               $i = $i + 1;               local $temp = $zr * $zi;               local $zr2 = $zr * $zr;               local $zi2 = $zi * $zi;               $zr = $zr2 - $zi2 + $cr;               $zi = $temp + $temp + $ci;               if ($zi2 + $zr2 > $BAILOUT)               {                       return $i;               }               if ($i > $MAX_ITERATIONS)               {                       return 0;               }       }}for ($y = -39; $y < 39; $y++){       print("/n");       for ($x = -39; $x < 39; $x++)       {               $i = mandelbrot($x/40.0, $y/40.0);               if ($i == 0)               {                       print("*");               }               else               {                       print(" ");               }       }}print("/n");$end = time() - $begin;print "Perl Elapsed $end/n";

PHP 5.1.4

#!/usr/local/php5/bin/php<?phpdefine("BAILOUT",16);define("MAX_ITERATIONS",1000);class Mandelbrot{function Mandelbrot(){$d1 = microtime(1);for ($y = -39; $y < 39; $y++) {echo("/n");for ($x = -39; $x < 39; $x++) {if ($this->iterate($x/40.0,$y/40.0) == 0) echo("*");elseecho(" ");}}$d2 = microtime(1);$diff = $d2 - $d1;printf("/nPHP Elapsed %0.2f", $diff);}function iterate($x,$y){$cr = $y-0.5;$ci = $x;$zi = 0.0;$zr = 0.0;$i = 0;while (true) {$i++;$temp = $zr * $zi;$zr2 = $zr * $zr;$zi2 = $zi * $zi;$zr = $zr2 - $zi2 + $cr;$zi = $temp + $temp + $ci;if ($zi2 + $zr2 > BAILOUT)return $i;if ($i > MAX_ITERATIONS)return 0;}}}$m = new Mandelbrot();?>

Ruby 1.8.4

#!/usr/local/bin/rubyBAILOUT = 16MAX_ITERATIONS = 1000class Mandelbrotdef initializeputs "Rendering"for y in -39...39 doputsfor x in -39...39 doi = iterate(x/40.0,y/40.0)if (i == 0)print "*"elseprint " "endendendenddef iterate(x,y)cr = y-0.5ci = xzi = 0.0zr = 0.0i = 0while(1)i += 1temp = zr * zizr2 = zr * zrzi2 = zi * zizr = zr2 - zi2 + crzi = temp + temp + cireturn i if (zi2 + zr2 > BAILOUT)return 0 if (i > MAX_ITERATIONS)endendendtime = Time.nowMandelbrot.newputsputs "Ruby Elapsed %f" % (Time.now - time)

Javascript SpiderMonkey v1.6

#!/usr/local/bin/js/* Javascript version by Patrick Haller.*/function mandelbrot(x, y) {var cr = y - 0.5;var ci = x;var zi = 0.0;var zr = 0.0;var i = 0;var BAILOUT = 16;var MAX_ITERATIONS = 1000;while(1) {i++;var temp = zr * zi;var zr2 = zr * zr;var zi2 = zi * zi;zr = zr2 - zi2 + cr;zi = temp + temp + ci;if (zi2 + zr2 > BAILOUT) {return i;}if (i > MAX_ITERATIONS) {return 0;}}}function mandelbrot_run() {var x; var y;output = "";var date = new Date();for (y = -39; y < 39; y++) {print(output);output = "";for (x = -39; x < 39; x++) {var i = mandelbrot(x/40.0, y/40.0);if (i==0) {output += "*";}else {output += " ";}}}var date2 = new Date();output += "/nJavaScript Elapsed " + (date2.getTime() - date.getTime()) / 1000;print(output);return false;}mandelbrot_run();

TCL 8.4 Optimized

#!/usr/bin/tclsh# Optimized Version by Samuel Zafrany# Ported from C by Anders Bergh <anders1@gmail.com>package require Tcl 8.4set BAILOUT 16set MAX_ITERATIONS 1000proc mandelbrot {x y} {       global BAILOUT       global MAX_ITERATIONS       set cr [expr {$y - 0.5}]       set ci $x       set zi 0.0       set zr 0.0       set i 0       while {1} {               incr i               set temp [expr {$zr * $zi}]               set zr2 [expr {$zr * $zr}]               set zi2 [expr {$zi * $zi}]               set zr [expr {$zr2 - $zi2 + $cr}]               set zi [expr {$temp + $temp + $ci}]               if {$zi2 + $zr2 > $BAILOUT} {                       return $i               }               if {$i > $MAX_ITERATIONS} {                       return 0               }       }}set begin [clock clicks -milliseconds]for {set y -39} {$y < 39} {incr y} {       puts ""       for {set x -39} {$x < 39} {incr x} {               set i [mandelbrot [expr {$x / 40.0}] [expr {$y / 40.0}]]               if {$i == 0} {                       puts -nonewline "*"               } else {                       puts -nonewline " "               }               flush stdout       }}puts ""set diff [expr [clock clicks -milliseconds] - $begin]#puts "Tcl Elapsed [expr int($diff / 1000)] seconds ($diff ms)"puts "Tcl Elapsed [expr int($diff / 1000)]"

Emacs Lisp

; By Erik Wrenholt; emacs -l Mandelbrot.lisp -batch(defun iterate (xx yy)(let* ((BAILOUT 16.0)(MAX_ITERATIONS 1000)(bail_val 0)(cr (- yy 0.5))(ci xx)(zi 0.0)(zr 0.0)(i 0))(while (and (< i MAX_ITERATIONS) (< bail_val BAILOUT))(setq i (+ 1 i))(setq temp (* zr zi))(setq zr2 (* zr zr))(setq zi2 (* zi zi))(setq zr (+ (- zr2 zi2) cr))(setq zi (+ temp temp ci))(setq bail_val (+ zi2 zr2)))i))(defun mandelbrot()(setq yy -39)(while (< yy 39)  (setq yy (+ 1 yy))  (setq xx -39)  (while (< xx 39)    (setq xx (+ 1 xx))        (if (= (iterate (/ xx 40.0) (/ yy 40.0)) 1000)(princ  "*")(princ  " ")   ))(princ  "/n")))(setq the-time (cadr (current-time)))(mandelbrot)(princ (format "Elapsed %d" (- (cadr (current-time)) the-time)))

Applescript

(* Applescript Version by Erik WrenholtI couldn't figure out how to write to stdoutso it buffers the output until the end. *)on unixTime()(do shell script "date +%s") as integerend unixTimeon iterate(x, y)set BAILOUT to 16set MAX_ITERATIONS to 1000set cr to y - 0.5set ci to xset zi to 0.0set zr to 0.0set i to 0repeat while i < MAX_ITERATIONSset i to i + 1set temp to zr * ziset zr2 to zr * zrset zi2 to zi * ziset zr to zr2 - zi2 + crset zi to temp + temp + ciif zi2 + zr2 > BAILOUT thenreturn iend ifend repeatreturn 0end iterateset t to unixTime()set mandelbrotString to ""set nl to (ASCII character 10)repeat with y from -39 to 38 by 1set mandelbrotString to mandelbrotString & nlrepeat with x from -39 to 38 by 1if iterate(x / 40.0, y / 40.0) = 0 thenset mandelbrotString to mandelbrotString & "*"elseset mandelbrotString to mandelbrotString & " "end ifend repeatend repeatset elapsed to unixTime() - tmandelbrotString & nl & "Time Elapsed " & elapsed

Io 20070410

#!/usr/local/bin/IoServer# by Erik Wrenholtiterator := Object clone do (bailout := 16max_iterations := 1000mandelbrot := method (x,y,cr := y - 0.5ci := xzi := 0.0zr := 0.0i := 0while (1,i = i + 1temp := zr * zizr2 := zr * zrzi2 := zi * zizr := zr2 - zi2 + crzi := temp + temp +ciif (zi2 + zr2 > bailout,return i)if (i > max_iterations,return 0)))print_set := method (writeln("Rendering...")for(y, -39, 38,write("/n")for(x, -39, 38,i := mandelbrot(x/40.0,y/40.0)if (i == 0, write("*"),write(" "))))))writelnwriteln ("Io Elapsed " .. Date secondsToRun(iterator print_set))

SBCL 1.0.2

; sbcl lisp version by mandeep singh(declaim (optimize (speed 3)))(defconstant +BAILOUT+ 16)(defconstant +MAX-ITERATIONS+ 1000)(defun mandelbrot (x y)  (declare (type single-float x y))  (let ((cr (- y 0.5))        (ci x)        (zi 0.0)        (zr 0.0))    (declare (type single-float cr ci zi zr))    (do ((i 0 (incf i)))        (nil)      (let* ((temp (the single-float (* zr zi)))             (zr2 (the single-float (* zr zr)))             (zi2 (the single-float (* zi zi))))        (declare (type single-float temp zr2 zi2)                 (type fixnum i))        (setq zr (the single-float (+ (- zr2 zi2) cr)))        (setq zi (the single-float (+ temp temp ci)))        (if (> (the single-float (+ zi2 zr2)) +BAILOUT+)            (return-from mandelbrot i))        (if (> i +MAX-ITERATIONS+)            (return-from mandelbrot 0)))))) (defun main ()   (let ((tstart) (tfinish))     (setq tstart (get-internal-real-time))     (do ((y -39 (incf y))) ((= (the fixnum y) 39))       (format t "~%")       (do ((x -39 (incf x)))   ((= (the fixnum x) 39)) (let ((i (mandelbrot (the single-float (/ x 40.0))      (the single-float (/ y 40.0)))))   (declare (type fixnum i x y))        (if (zerop i)            (format t "*")            (format t " ")))))     (format t "~%")     (setq tfinish (get-internal-real-time))     (format t "SBCL Elapsed ~,2F~%" (coerce (/ (- tfinish tstart) internal-time-units-per-second) 'float))))(progn  (main) (quit))    
转自:http://www.timestretch.com/FractalBenchmark.html

原创粉丝点击