Skip to content

Instantly share code, notes, and snippets.

@kamawanu
Created September 10, 2010 18:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamawanu/574135 to your computer and use it in GitHub Desktop.
Save kamawanu/574135 to your computer and use it in GitHub Desktop.
http://www.timestretch.com/FractalBenchmark.html #pl,c,py,lua,io,php,gs,java,ocaml
#!/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";
#!/usr/local/bin/io
# Vectorized by Steve Dekorte
printSet := 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) *= zi
zr2 copy(zr) square
zi2 copy(zi) square
zr copy(zr2) -= zi2
zr += cr
zi copy(temp) *= 2
zi += ci
)
result := zi2 + zr2
i := 0
for(y, -39, 38,
writeln
for(x, -39, 38,
r := result at(i)
write(if( r > 0, "*", " "))
i = i + 1
)
)
)
writeln("\nIo Elapsed " .. Date secondsToRun(printSet))
// by Erik Wrenholt
#include <stdio.h>
#include <sys/time.h>
#define BAILOUT 16
#define MAX_ITERATIONS 1000
int 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("*");
else
printf(" ");
}
}
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;
}
%!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 def
realtime 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 def
y 40.0 div x 40.0 div mandelbrot
0 eq { star } { space } ifelse
} { exit } ifelse
} loop
} { exit }ifelse % Done.
} loop
realtime dup log ceiling cvi string cvs
(\nTotal time Elapsed ) print print (ms\n) print
quit
// by Erik Wrenholt
import 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("*");
else
System.out.print(" ");
}
}
Date d2 = new Date();
long diff = d2.getTime() - d1.getTime();
System.out.println("\nJava Elapsed " + diff/1000.0f);
}
}
#!/usr/local/bin/lua
-- By Erik Wrenholt
local BAILOUT = 16
local MAX_ITERATIONS = 1000
function iterate(x,y)
local cr = y-0.5
local ci = x
local zi = 0.0
local zr = 0.0
local i = 0
while 1 do
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) then
return i
end
if (i > MAX_ITERATIONS) then
return 0
end
end
end
function mandelbrot()
local t = os.time()
for y = -39, 38 do
for x = -39, 38 do
if (iterate(x/40.0, y/40) == 0) then
io.write("*")
else
io.write(" ")
end
end
io.write("\n")
end
io.write(string.format("Time Elapsed %d\n", os.time() - t))
end
mandelbrot()
(* Courtesy of pango on #ocaml *)
let bailout = 16.
let max_iterations = 1000
let 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;
!result
let () =
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
#!/usr/local/php5/bin/php
<?php
define("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("*");
else
echo(" ");
}
}
$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();
#!/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";
#!/usr/local/bin/python
# by Daniel Rosengren
import sys, time
stdout = sys.stdout
BAILOUT = 16
MAX_ITERATIONS = 1000
class 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 0
t = time.time()
Iterator()
print '\nPython Elapsed %.02f' % (time.time() - t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment