Home | Fractal Benchmark
Fractal Benchmark Programs
C GCC 4.3.2
// 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;
}
Java
// by Erik Wrenholt, altered to double float arithmetic by Theo Wollenleben
import java.util.*;
class Mandelbrot
{
static int BAILOUT = 16;
static int MAX_ITERATIONS = 1000;
private static int iterate(double x, double y)
{
double cr = y-0.5f;
double ci = x;
double zi = 0.0f;
double zr = 0.0f;
int i = 0;
while (true) {
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;
}
}
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);
}
}
Lua 5.1.4
#!/usr/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()
Objective Caml 3.10.2
(*#!/opt/local/bin/ocamlrun ocaml unix.cma*)
(* Linux doesn't split the arguments in the shebang line.
Alternatives (times don't differ):
ocaml unix.cma Mandelbrot.ml
ocamlrun ocaml unix.cma Mandelbrot.ml
ocamlc unix.cma Mandelbrot.ml -o Mandelbrot; ./Mandelbrot
Compiled:
ocamlopt unix.cmxa Mandelbrot.ml -o Mandelbrot; ./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
Python 2.6.0
#!/usr/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)
PostScript Ghostscript 8.62
%!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
Perl 5.10.0 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.10.0
#!/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.2.9
#!/usr/bin/php5
<?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();
?>
Ruby 1.8.7
#!/usr/bin/ruby
BAILOUT = 16
MAX_ITERATIONS = 1000
class Mandelbrot
def initialize
puts "Rendering"
for y in -39...39 do
puts
for x in -39...39 do
i = iterate(x/40.0,y/40.0)
if (i == 0)
print "*"
else
print " "
end
end
end
end
def iterate(x,y)
cr = y-0.5
ci = x
zi = 0.0
zr = 0.0
i = 0
while(1)
i += 1
temp = zr * zi
zr2 = zr * zr
zi2 = zi * zi
zr = zr2 - zi2 + cr
zi = temp + temp + ci
return i if (zi2 + zr2 > BAILOUT)
return 0 if (i > MAX_ITERATIONS)
end
end
end
time = Time.now
Mandelbrot.new
puts
puts "Ruby Elapsed %f" % (Time.now - time)
JavaScript SpiderMonkey 1.7.0
#!/usr/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.5.5
#!/usr/bin/tclsh
# Optimized Version by Samuel Zafrany
# Ported from C by Anders Bergh <anders1@gmail.com>
package require Tcl 8.4
set BAILOUT 16
set MAX_ITERATIONS 1000
proc 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 22.3 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)))
Io 20080330
#!/usr/bin/io
# by Erik Wrenholt
iterator := Object clone do (
bailout := 16
max_iterations := 1000
mandelbrot := method (x,y,
cr := y - 0.5
ci := x
zi := 0.0
zr := 0.0
i := 0
while (1,
i = 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)
)
)
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(" "))
)
)
)
)
writeln
writeln ("Io Elapsed " .. Date secondsToRun(iterator print_set))
Common Lisp CMUCL 19f
; sbcl lisp version by mandeep singh, altered to double float arithmetic by Theo Wollenleben
(declaim (optimize (speed 3)))
(defconstant +BAILOUT+ 16)
(defconstant +MAX-ITERATIONS+ 1000)
(defun mandelbrot (x y)
(declare (type double-float x y))
(let ((cr (- y 0.5))
(ci x)
(zi 0d0)
(zr 0d0))
(declare (type double-float cr ci zi zr))
(do ((i 0 (incf i)))
(nil)
(let* ((temp (the double-float (* zr zi)))
(zr2 (the double-float (* zr zr)))
(zi2 (the double-float (* zi zi))))
(declare (type double-float temp zr2 zi2)
(type fixnum i))
(setq zr (the double-float (+ (- zr2 zi2) cr)))
(setq zi (the double-float (+ temp temp ci)))
(if (> (the double-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 double-float (/ x 40d0))
(the double-float (/ y 40d0)))))
(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))
GNU AWK 3.1.6
#!/bin/gawk -f
# by Erik Wrenholt, ported from C to GNU AWK 3.1.6 by Theo Wollenleben
function mandelbrot(x, y)
{
cr = y - 0.5
ci = x
zi = 0.0
zr = 0.0
i = 0
while(1) {
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
}
}
BEGIN {
BAILOUT = 16
MAX_ITERATIONS = 1000
init_time=systime()
for (y = -39; y < 39; y++) {
printf("\n")
for (x = -39; x < 39; x++) {
i = mandelbrot(x/40.0, y/40.0)
if (i==0)
printf("*")
else
printf(" ")
}
}
printf("\n")
query_time = systime() - init_time
printf("GAWK Elapsed %d\n", query_time)
exit
}
GNU bc 1.06
#!/usr/bin/bc -ql
# BC_LINE_LENGTH=80 ./Mandelbrot.bc
# by Erik Wrenholt, ported from C to GNU bc 1.06 by Theo Wollenleben
# bc has no time functions
scale=16
bailout = 16
max_iterations = 1000
define mandelbrot(x, y) {
cr = y - 0.5
ci = x
zi = 0.0
zr = 0.0
i = 0
while(1) {
i = 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 (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"
quit
XSLT libxslt 1.1.24
<?xml version="1.0" encoding="utf-8"?>
<!-- echo '<x/>'|xsltproc Mandelbrot.xslt - -->
<!-- by Erik Wrenholt, ported from C to XSLT by Theo Wollenleben -->
<!-- no time functions in XSLT -->
<!-- there is a 'timing' option for xsltproc, see also http://exslt.org/date/ -->
<!-- no output until xsltproc has finished -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="no"/>
<xsl:variable name="BAILOUT" select="16"/>
<xsl:variable name="MAX_ITERATIONS" select="1000"/>
<xsl:template name="while">
<xsl:param name="i"/>
<xsl:param name="temp"/>
<xsl:param name="zr2"/>
<xsl:param name="zi2"/>
<xsl:param name="cr"/>
<xsl:param name="ci"/>
<xsl:if test="$zi2 + $zr2 <= $BAILOUT and $i <= $MAX_ITERATIONS">
<xsl:call-template name="mandelbrot">
<xsl:with-param name="cr" select="$cr"/>
<xsl:with-param name="ci" select="$ci"/>
<xsl:with-param name="zr" select="$zr2 - $zi2 + $cr"/>
<xsl:with-param name="zi" select="$temp + $temp + $ci"/>
<xsl:with-param name="i" select="$i"/>
</xsl:call-template>
</xsl:if>
<xsl:if test="$zi2 + $zr2 > $BAILOUT">
<xsl:text> </xsl:text>
</xsl:if>
<xsl:if test="$i > $MAX_ITERATIONS">
<xsl:text>*</xsl:text>
</xsl:if>
</xsl:template>
<xsl:template name="mandelbrot">
<xsl:param name="cr"/>
<xsl:param name="ci"/>
<xsl:param name="zr"/>
<xsl:param name="zi"/>
<xsl:param name="i"/>
<xsl:call-template name="while">
<xsl:with-param name="i" select="$i + 1"/>
<xsl:with-param name="temp" select="$zr * $zi"/>
<xsl:with-param name="zr2" select="$zr * $zr"/>
<xsl:with-param name="zi2" select="$zi * $zi"/>
<xsl:with-param name="cr" select="$cr"/>
<xsl:with-param name="ci" select="$ci"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="for_x">
<xsl:param name="x"/>
<xsl:param name="y"/>
<xsl:if test="$x < 39">
<xsl:call-template name="mandelbrot">
<xsl:with-param name="cr" select="$y div 40 - 0.5"/>
<xsl:with-param name="ci" select="$x div 40"/>
<xsl:with-param name="zr" select="0.0"/>
<xsl:with-param name="zi" select="0.0"/>
<xsl:with-param name="i" select="0"/>
</xsl:call-template>
<xsl:call-template name="for_x">
<xsl:with-param name="x" select="$x + 1"/>
<xsl:with-param name="y" select="$y"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="for_y">
<xsl:param name="y"/>
<xsl:if test="$y < 39">
<xsl:value-of select="' '"/>
<xsl:call-template name="for_x">
<xsl:with-param name="x" select="-39"/>
<xsl:with-param name="y" select="$y"/>
</xsl:call-template>
<xsl:call-template name="for_y">
<xsl:with-param name="y" select="$y + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="/">
<xsl:call-template name="for_y">
<xsl:with-param name="y" select="-39"/>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
tcsh 6.15.00
#!/usr/bin/tcsh
# by Erik Wrenholt, ported from C to tcsh 6.15.00 by Theo Wollenleben
# 32 bit signed integer arithmetic, output differs slightly
@ SCALE = (1 << 24) SCALE2 = (1 << 12)
@ BAILOUT = 16 * $SCALE
set MAX_ITERATIONS = 1000
alias mandelbrot 'set x = \!:1 set y = \!:2 \
\
@ cr = $y - $SCALE / 2 \
set ci = $x \
set zi = 0 \
set zr = 0 \
set i = 0 \
\
while (1) \
@ i ++ \
@ temp = ($zr / $SCALE2) * ($zi / $SCALE2) \
@ zr2 = ($zr / $SCALE2) * ($zr / $SCALE2) \
@ zi2 = ($zi / $SCALE2) * ($zi / $SCALE2) \
@ zr = ($zr2 - $zi2) + $cr \
@ zi = ($temp + $temp) + $ci \
if ($zi2 + $zr2 > $BAILOUT) then \
echo $i; break \
endif \
if ($i > $MAX_ITERATIONS) then \
echo 0; break \
endif \
end'
set y = -39
while ($y < 39)
echo
set x = -39
while ($x < 39)
@ arg_x = ($x * $SCALE) / 40
@ arg_y = ($y * $SCALE) / 40
set i = `mandelbrot $arg_x $arg_y`
if ($i == 0) then
echo -n "*"
else
echo -n " "
endif
@ x ++
end
@ y ++
end
echo
set time = ( 0 "%U+%S" )
echo -n "tcsh Elapsed "
time
dash 0.5.5.1
#!/usr/local/bin/dash
# by Erik Wrenholt, ported from C to dash 0.5.5.1 by Theo Wollenleben
# 64 bit signed integer precision
SCALE=$((1<<56)); SCALE2=$((1<<28))
BAILOUT=$((16*SCALE))
MAX_ITERATIONS=1000
mandelbrot ()
{ x=$1 y=$2
cr=$((y-SCALE/2))
ci=$x
zi=0
zr=0
i=0
while true; do
i=$((i+1))
temp=$(((zr/SCALE2)*(zi/SCALE2)))
zr2=$(((zr/SCALE2)*(zr/SCALE2)))
zi2=$(((zi/SCALE2)*(zi/SCALE2)))
zr=$((zr2-zi2+cr))
zi=$((temp+temp+ci))
if [ $((zi2+zr2)) -gt $BAILOUT ]; then
printf $i; return
fi
if [ $i -gt $MAX_ITERATIONS ]; then
printf 0; return
fi
done
}
y=-39
while [ $y -lt 39 ]; do
echo
x=-39
while [ $x -lt 39 ]; do
i=$(mandelbrot $((x*SCALE/40)) $((y*SCALE/40)))
if [ $i -eq 0 ]; then
printf "*"
else
printf " "
fi
x=$((x+1))
done
y=$((y+1))
done
echo
echo dash Elapsed
times
cmd.exe Windows XP
@echo off
rem by Erik Wrenholt, ported from C to Windows XP cmd.exe by Theo Wollenleben
setlocal EnableDelayedExpansion
rem 32 bit signed integer precision, output differs slightly
set /a SCALE="1<<24"
set /a SCALE2="1<<12"
set /a BAILOUT = 16 * SCALE
set MAX_ITERATIONS=1000
call:main & endlocal & goto:eof
rem wall clock time in seconds
:wall_time
for /f "tokens=3-5 delims=:. " %%a in ('echo.^|time') do (
set /a %1 = ^(%%a*60+%%b^)*60+%%c & goto:eof)
goto:eof
:mandelbrot
set x=%2 & set y=%3
set /a cr = y - SCALE / 2
set /a ci = x
set zi=0
set zr=0
set i=0
:while
set /a i += 1
set /a temp = (zr / SCALE2) * (zi / SCALE2)
set /a zr2 = (zr / SCALE2) * (zr / SCALE2)
set /a zi2 = (zi / SCALE2) * (zi / SCALE2)
set /a zr = zr2 - zi2 + cr
set /a zi = temp + temp + ci
set /a r2 = zi2 + zr2
if %r2% gtr %BAILOUT% (
set /a %1 = %i% & goto:eof)
if %i% gtr %MAX_ITERATIONS% (
set /a %1 = 0 & goto:eof)
goto while
goto:eof
:main
call:wall_time init_time
for /l %%y in (-39, 1, 38) do (
echo.
for /l %%x in (-39, 1, 38) do (
set /a arg_x = %%x * SCALE / 40
set /a arg_y = %%y * SCALE / 40
call:mandelbrot i !arg_x! !arg_y!
if !i! equ 0 (
<nul set/p output="*"
) else (
<nul set/p output= )
)
)
echo.
call:wall_time final_time
set /a query_time = final_time - init_time
echo cmd Elapsed %query_time%
goto:eof
bash 3.2
#!/bin/bash
# by Erik Wrenholt, ported from C to GNU bash 3.2 by Theo Wollenleben
# 64 bit signed integer precision
declare -i SCALE=2**56 SCALE2=2**28
declare -i BAILOUT=16*SCALE
declare -i MAX_ITERATIONS=1000
function mandelbrot
{ local -i x=$1 y=$2
local -i cr=y-SCALE/2 ci=x
local -i zi=0 zr=0 i=0 temp zr2 zi2
while true; do
i=i+1
temp=\(zr/SCALE2\)*\(zi/SCALE2\)
zr2=\(zr/SCALE2\)*\(zr/SCALE2\)
zi2=\(zi/SCALE2\)*\(zi/SCALE2\)
zr=zr2-zi2+cr
zi=temp+temp+ci
if [[ zi2+zr2 -gt BAILOUT ]]; then
printf $i; return
fi
if [[ i -gt MAX_ITERATIONS ]]; then
printf 0; return
fi
done
}
declare -i x y
for ((y = -39; y < 39; y++)); do
echo
for ((x = -39; x < 39; x++)); do
declare -i i=$(mandelbrot $((x*SCALE/40)) $((y*SCALE/40)))
if [[ i -eq 0 ]]; then
printf "*"
else
printf " "
fi
done
done
echo
export LC_NUMERIC=C
times > >(read -d "" TIMES
TIMES=${TIMES//[ $'\n']/+}
TIMES=${TIMES//[s.]/}
TIMES=${TIMES//m/*60000+10#}
TIMES=$(printf "%03d" $(($TIMES)))
FLOOR=${TIMES%???}
printf "bash Elapsed %0.2f\n" $FLOOR.${TIMES#$FLOOR})
KornShell 93t
#!/usr/bin/ksh
# by Erik Wrenholt, ported from C to KornShell 93t by Theo Wollenleben
# arithmetic evaluation depends on language settings
export LANG; LANG=en_EN.UTF-8
typeset -E BAILOUT=16
typeset -i MAX_ITERATIONS=1000
# CPU time in seconds
# ksh ignores TIMEFORMAT if 'time' is called without pipeline
function cpu_time
{
typeset t=$({ time;} 2>&1)
t=${t//~(E:(user|sys|s|\t))/}
t=${t//h/*3600+}; t=${t//m/*60+}
printf $((${t/$'\n'/+}))
}
function mandelbrot
{ typeset -E x=$1 y=$2
typeset -E cr=y-0.5 ci=x
typeset -E zi=0.0 zr=0.0 temp zr2 zi2
typeset -i i=0
while true; do
i=i+1
temp=zr*zi
zr2=zr*zr
zi2=zi*zi
zr=zr2-zi2+cr
zi=temp+temp+ci
if ((zi2 + zr2 > BAILOUT)); then
printf $i; return
fi
if ((i > MAX_ITERATIONS)); then
printf 0; return
fi
done
}
init_time=$(cpu_time)
typeset -i x y
for ((y = -39; y < 39; y++)); do
printf "\n"
for ((x = -39; x < 39; x++)); do
typeset -i i=$(mandelbrot $((x/40.0)) $((y/40.0)))
if ((i == 0)); then
printf "*"
else
printf " "
fi
done
done
printf "\n"
printf "KornShell Elapsed %0.2f\n" $(($(cpu_time)-init_time))
Z shell 4.3.6
#!/usr/bin/zsh
# by Erik Wrenholt, ported from C to Z shell 4.3.6 by Theo Wollenleben
float BAILOUT=16
integer MAX_ITERATIONS=1000
function mandelbrot
{ float x=$1 y=$2
float cr=y-0.5 ci=x
float zi=0.0 zr=0.0 temp zr2 zi2
integer i=0
while true; do
i=i+1
temp=zr*zi
zr2=zr*zr
zi2=zi*zi
zr=zr2-zi2+cr
zi=temp+temp+ci
if ((zi2 + zr2 > BAILOUT)); then
printf $i; return
fi
if ((i > MAX_ITERATIONS)); then
printf 0; return
fi
done
}
integer x y
for ((y = -39; y < 39; y++)); do
print
for ((x = -39; x < 39; x++)); do
integer i=$(mandelbrot $((x/40.0)) $((y/40.0)))
if ((i == 0)); then
printf "*"
else
printf " "
fi
done
done
print
export TIMEFMT="%U+%S" LC_NUMERIC=C
{ time;} 2> >(
read -d "" TIME
printf "zsh Elapsed %0.2f\n" $((${${TIME//s/}/$'\n'/+}))
)
Mathematica 5.2
#!/usr/local/bin/math -initfile
(* by Erik Wrenholt, ported from C to Mathematica 5.2 by Theo Wollenleben *)
$MinPrecision = $MaxPrecision = $MachinePrecision
BAILOUT = 16;
MAXITERATIONS = 1000;
mandelbrot[x_, y_] := Module[
{cr = y - 0.5,
ci = x,
zi = 0.0,
zr = 0.0,
i = 0,
temp, zr2, zi2},
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 > MAXITERATIONS,
Return[0]]
]
];
inittime = TimeUsed[];
For[y = -39, y < 39, y++,
Print[];
For[x = -39, x < 39, x++,
i = mandelbrot[x/40.0, y/40.0];
If[i==0,
WriteString["stdout", "*"],
WriteString["stdout", " "]]
]
]
Print[];
querytime = TimeUsed[] - inittime;
Print["Mathematica Elapsed ", querytime];
Quit[]
Mathematica 5.2 compiled
#!/usr/local/bin/math -initfile
(* by Erik Wrenholt, ported from C to Mathematica by Theo Wollenleben *)
$MinPrecision = $MaxPrecision = $MachinePrecision
BAILOUT = 16;
MAXITERATIONS = 1000;
mandelbrot = Compile[{x, y}, Module[
{cr = y - 0.5,
ci = x,
zi = 0.0,
zr = 0.0,
i = 0,
temp, zr2, zi2},
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 > MAXITERATIONS,
Return[0]]
];
Return[0]
]];
inittime = TimeUsed[];
For[y = -39, y < 39, y++,
Print[];
For[x = -39, x < 39, x++,
i = mandelbrot[x/40.0, y/40.0];
If[i==0,
WriteString["stdout", "*"],
WriteString["stdout", " "]]
]
];
Print[];
querytime = TimeUsed[] - inittime;
Print["Mathematica Elapsed ", querytime]
Quit[]
Maple 10
#!/opt/maple10/bin/maple -q
# by Erik Wrenholt, ported from C to Maple 10.00 by Theo Wollenleben
# may crash randomly with odd error "Execution stopped: Stack limit reached."
# using kernelopts(stacklimit=...) doesn't help
Digits := 16:
BAILOUT := 16:
MAX_ITERATIONS := 1000:
mandelbrot := proc(x, y)
local cr, ci, zi, zr, i, temp, zr2, zi2;
cr := y - 0.5:
ci := x:
zi := 0.0:
zr := 0.0:
i := 0:
while true do
i := i + 1:
temp := zr * zi:
zr2 := zr * zr:
zi2 := zi * zi:
zr := zr2 - zi2 + cr:
zi := temp + temp + ci:
if zi2 + zr2 > BAILOUT then
return i
end if;
if i > MAX_ITERATIONS then
return 0
end if
end do
end proc:
init_time := time():
for y from -39 to 38 do
printf("\n");
for x from -39 to 38 do
i := mandelbrot(x/40.0, y/40.0);
if i=0 then
printf("*")
else
printf(" ")
end if;
end do;
end do;
printf("\n");
query_time := time() - init_time:
printf("Maple Elapsed %0.3f\n", query_time);
Maple 10 compiled GCC 4.3.2
#!/opt/maple10/bin/maple -q
# by Erik Wrenholt, ported from C to Maple 10.00 by Theo Wollenleben
BAILOUT := 16:
MAX_ITERATIONS := 1000:
mandelbrot := proc(x, y)
local cr, ci, zi, zr, i, temp, zr2, zi2;
cr := y - 0.5:
ci := x:
zi := 0.0:
zr := 0.0:
i := 0:
while true do
i := i + 1:
temp := zr * zi:
zr2 := zr * zr:
zi2 := zi * zi:
zr := zr2 - zi2 + cr:
zi := temp + temp + ci:
if zi2 + zr2 > BAILOUT then
return i
end if;
if i > MAX_ITERATIONS then
return 0
end if
end do
end proc:
cmandelbrot := Compiler:-Compile(mandelbrot, optimize=true):
init_time := time():
for y from -39 to 38 do
printf("\n");
for x from -39 to 38 do
i := cmandelbrot(x/40.0, y/40.0);
if i=0 then
printf("*")
else
printf(" ")
end if;
end do;
end do;
printf("\n");
query_time := time() - init_time:
printf("Maple Elapsed %0.3f\n", query_time);
Maxima 5.1
/* maxima --very-quiet < Mandelbrot.mac */
/* by Erik Wrenholt, ported from C to Maxima 5.17.1 by Theo Wollenleben */
/* needed for compilation: */
mode_declare(
BAILOUT,fixnum,
MAX_ITERATIONS,fixnum,
i,fixnum
)$
i: 0$
/* maxima -r 'compile_file("Mandelbrot.mac");' */
/* maxima -q --batch-lisp=Mandelbrot.fas */
BAILOUT: 16$
MAX_ITERATIONS: 1000$
mandelbrot(x, y) :=
block(
[cr: y - 0.5,
ci: x,
zi: 0.0,
zr: 0.0,
i: 0,
temp, zr2, zi2
],
while true do (
i: i + 1,
temp: zr * zi,
zr2: zr * zr,
zi2: zi * zi,
zr: zr2 - zi2 + cr,
zi: temp + temp + ci,
if zi2 + zr2 > BAILOUT then
return(i),
if i > MAX_ITERATIONS then
return(0)
)
)$
for y: -39 thru 38 step 1 do (
printf(true, "~%"),
for x: -39 thru 38 do (
i: mandelbrot(x/40.0, y/40.0),
if i=0 then
printf(true, "*")
else
printf(true, " ")
)
)$
print("")$
query_time: elapsed_run_time ()$
printf(true, "Maxima Elapsed ~,2f~%", query_time)$
quit()$
Axiom jan2009
-- su -c 'axiom -noht'
-- )read Mandelbrot )quiet
-- need to run as root only if axiom was installed as root
-- by Erik Wrenholt, ported from C to Axiom by Theo Wollenleben
)set messages autoload off
)set message time on
)set messages type off
)set output length 79
)set quit unprotected
macro BAILOUT == 16;
macro MAX_ITERATIONS == 1000;
mandelbrot(x: DoubleFloat, y: DoubleFloat): Integer ==
cr : DoubleFloat
cr := y - 0.5
ci : DoubleFloat
ci := x
zi : DoubleFloat := 0.0
zr : DoubleFloat := 0.0
i : Integer := 0
temp : DoubleFloat
zr2 : DoubleFloat
zi2 : DoubleFloat
while true repeat
i := i + 1
temp := zr * zi
zr2 := zr * zr
zi2 := zi * zi
zr := zr2 - zi2 + cr
zi := temp + temp + ci
if zi2 + zr2 > BAILOUT then
return i
if i > MAX_ITERATIONS then
return 0
x : Integer
x : Integer
i : Integer
line : String := ""; -- I found no way to print without newline
for y in -39..38 repeat
output(line); line := ""
for x in -39..39 repeat
i := mandelbrot(x/40.0, y/40.0)
if i=0 then
line := concat(line, "*")
else
line := concat(line, " ")
output("")
)quit
Yacas 1.2.2
// yacas -pc Mandelbrot.ys
// by Erik Wrenholt, ported from C to Yacas 1.2.2 by Theo Wollenleben
Builtin'Precision'Set(16);
BAILOUT := 16;
MAXITERATIONS := 1000;
Function("mandelbrot", {x, y})
[ Local(cr,ci,zi,zr,i,temp,zr2,zi2);
cr := y - 0.5;
ci := If(x=0,1e-85,x); // avoid very slow arithmetic with zero
zi := 0.0;
zr := 0.0;
i := 0;
Until(zi2 + zr2 > BAILOUT Or i > MAXITERATIONS) [
i ++;
temp := zr * zi;
zr2 := zr * zr;
zi2 := zi * zi;
zr := zr2 - zi2 + cr;
zi := temp + temp + ci;
];
If (i > MAXITERATIONS,
i := 0);
i;
];
querytime := GetTime([
For (y := -39, y < 39, y++) [
NewLine();
For (x := -39, x < 39, x++) [
i := mandelbrot(MathDivide(x,40.0), MathDivide(y,40.0));
If (i=0,
WriteString("*"),
WriteString(" ")
);
];
];
NewLine();
]);
WriteString("Yacas Elapsed");
Echo(N(querytime,2));
Yacas 1.2.2 core
// yacas -pc Mandelbrot-core.ys
// by Erik Wrenholt, ported from C to Yacas 1.2.2 by Theo Wollenleben
// uses built-in (core) functions
Builtin'Precision'Set(16);
BAILOUT := 16;
MAXITERATIONS := 1000;
Function("mandelbrot", {x, y})
[ Local(cr,ci,zi,zr,i,temp,zr2,zi2);
cr := MathSubtract(y,0.5);
ci := If(x=0,1e-85,x); // avoid very slow arithmetic with zero
zi := 0.0;
zr := 0.0;
i := 0;
Until(Or(GreaterThan(MathAdd(zi2,zr2),BAILOUT),
GreaterThan(i,MAXITERATIONS))) [
i := MathAdd(i,1);
temp := MathMultiply(zr,zi);
zr2 := MathMultiply(zr,zr);
zi2 := MathMultiply(zi,zi);
zr := MathAdd(MathSubtract(zr2,zi2),cr);
zi := MathAdd(MathAdd(temp,temp),ci);
];
If (GreaterThan(i,MAXITERATIONS),
i := 0);
i;
];
querytime := GetTime([
For (y := -39, LessThan(y,39), y := MathAdd(y,1)) [
NewLine();
For (x := -39, LessThan(x,39), x := MathAdd(x,1)) [
i := mandelbrot(MathDivide(x,40.0), MathDivide(y,40.0));
If (Equals(i,0),
WriteString("*"),
WriteString(" ")
);
];
];
NewLine();
]);
WriteString("Yacas Elapsed");
Echo(N(querytime,2));
Singular 3.0.4
// Singular -q --ticks-per-sec=100 ./Mandelbrot.sing
// by Erik Wrenholt, ported from C to Singular 3.0.4 by Theo Wollenleben
ring r = (real,16),x,dp; // double precision floating point numbers
number BAILOUT=16;
int MAX_ITERATIONS=1000;
proc mandelbrot (x, y)
{
number cr = y - 0.5;
number ci = x;
number zi = 0.0;
number zr = 0.0;
int i = 0;
number temp,zr2,zi2;
while(1) {
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); }
}
}
number init_time = timer;
int x,y,i;
string line; // I found no way to print without newline
for (y = -39; y < 39; y++) {
print(line); line="";
for (x = -39; x < 39; x++) {
i = mandelbrot(x/40.0, y/40.0);
if (i==0) {
line=line+sprintf("*"); }
else {
line=line+sprintf(" "); }
}
}
print ("");
number query_time = (timer - init_time)/system("--ticks-per-sec");
printf ("%s%s", "Singular Elapsed ", query_time);
quit;
GAP 4
# gap -q -L /opt/gap4r4/bin/wsgap4 < Mandelbrot.g
# by Erik Wrenholt, ported from C to GAP 4 by Theo Wollenleben
# no floating point arithmetic
# SCALE is large enough such that output image does not differ
SCALE:=2^68;; SCALE2:=2^34;;
BAILOUT:=16*SCALE;;
MAX_ITERATIONS:=1000;;
mandelbrot:= function(x, y)
local cr, ci, zi, zr, i, temp, zr2, zi2;
cr := y - QuoInt(SCALE,2);
ci := x;
zi := 0;
zr := 0;
i := 0;
while true do
i := i + 1;
temp := QuoInt(zr,SCALE2) * QuoInt(zi,SCALE2);
zr2 := QuoInt(zr,SCALE2) * QuoInt(zr,SCALE2);
zi2 := QuoInt(zi,SCALE2) * QuoInt(zi,SCALE2);
zr := zr2 - zi2 + cr;
zi := temp + temp + ci;
if zi2 + zr2 > BAILOUT then
return i; fi;
if i > MAX_ITERATIONS then
return 0; fi;
od;
end;;
aTv := Runtimes();;
init_time := aTv.user_time+aTv.system_time+aTv.user_time_children+aTv.system_time_children;;
for y in [-39..38] do
Print("\n");
for x in [-39..38] do
i := mandelbrot(QuoInt(x*SCALE,40), QuoInt(y*SCALE,40));
if i=0 then
Print("*\c");
else
Print(" \c");
fi;
od;
od;
Print ("\n");
aTv := Runtimes();;
query_time := aTv.user_time+aTv.system_time+aTv.user_time_children+aTv.system_time_children-init_time;;
query_time_string := ReplacedString(String(query_time,4)," ","0");;
Add(query_time_string,'.',Length(query_time_string)-2);
Print("GAP Elapsed ",query_time_string,"\n");
quit;
Octave 3.0.3
#!/usr/bin/octave -q
# by Erik Wrenholt, ported from C to Octave 3.0.3 by Theo Wollenleben
global BAILOUT = 16;
global MAX_ITERATIONS = 1000;
function i = mandelbrot (x, y)
global BAILOUT MAX_ITERATIONS
cr = y - 0.5;
ci = x;
zi = 0.0;
zr = 0.0;
i = 0;
while(1)
i ++;
temp = zr * zi;
zr2 = zr * zr;
zi2 = zi * zi;
zr = zr2 - zi2 + cr;
zi = temp + temp + ci;
if (zi2 + zr2 > BAILOUT)
return;
endif
if (i > MAX_ITERATIONS)
i = 0; return;
endif
endwhile
endfunction
init_time = cputime () (1);
for y = -39:38
printf("\n");
for x = -39:38
i = mandelbrot(x/40.0, y/40.0);
if (i==0)
printf("*");
else
printf(" ");
endif
endfor
endfor
printf ("\n");
query_time = cputime () (1) - init_time;
printf ("Octave Elapsed %0.2f\n", query_time);
Scilab 5.1
// scilab -nogui -nwni -nb -nouserstartup -f Mandelbrot.sci
// by Erik Wrenholt, ported from C to Scilab 5.1 by Theo Wollenleben
BAILOUT = 16;
MAX_ITERATIONS = 1000;
function i = mandelbrot (x, y)
cr = y - 0.5;
ci = x;
zi = 0.0;
zr = 0.0;
i = 0;
while 1
i = i + 1;
temp = zr * zi;
zr2 = zr * zr;
zi2 = zi * zi;
zr = zr2 - zi2 + cr;
zi = temp + temp + ci;
if zi2 + zr2 > BAILOUT
return;
end
if i > MAX_ITERATIONS
i = 0; return;
end
end
endfunction
init_time = timer();
for y = -39:38
printf('\n');
for x = -39:38
i = mandelbrot(x/40.0, y/40.0);
if (i==0)
printf("*");
else
printf(" ");
end
end
end
printf ("\n");
query_time = timer() - init_time;
printf ("Scilab Elapsed %0.2f\n", query_time);
quit
Haskell
-- ghc --make Mandelbrot.hs; ./Mandelbrot
-- runghc ./Mandelbrot.hs # remove output of ghc before!
-- runhugs ./Mandelbrot.hs
-- by Erik Wrenholt, ported to Haskell by Brad Clawsie
-- completed by Theo Wollenleben
module Main where
import System.CPUTime
import Text.Printf
mandelbrot :: Double -> Double -> Int
mandelbrot x y =
while 0.0 0.0 0
where bailout = 16
maxIterations = 1000
cr = y - 0.5
ci = x
while :: Double -> Double -> Int -> Int
while zr zi i =
let i' = succ i
temp = zr * zi
zr2 = zr * zr
zi2 = zi * zi
zr' = zr2 - zi2 + cr
zi' = temp + temp + ci in
case (zi2 + zr2 > bailout) of
True -> i'
False -> case (i' > maxIterations) of
True -> 0
False -> while zr' zi' i'
main = do
init_time <- getCPUTime
let i = map (map (\(x,y) -> mandelbrot (x/40.0) (y/40.0)))
[[(x,y) | x <- [-39..38]] | y <- [-39..38]]
mapM putStr (map (('\n':).(map (\i -> if i==0 then '*' else ' '))) i)
putStr "\n"
final_time <- getCPUTime
let query_time = final_time - init_time
printf "Haskell Elapsed %0.2f\n" ((fromIntegral query_time)/10^12::Double)
Haskell jhc 0.6.0 GCC 4.3.2
-- ghc --make Mandelbrot.hs; ./Mandelbrot
-- runghc ./Mandelbrot.hs # remove output of ghc before!
-- runhugs ./Mandelbrot.hs
-- by Erik Wrenholt, ported to Haskell by Brad Clawsie
-- completed by Theo Wollenleben
module Main where
-- import System.CPUtime -- missing in jhc 0.6.0
import Text.Printf
mandelbrot :: Double -> Double -> Int
mandelbrot x y =
while 0.0 0.0 0
where bailout = 16
maxIterations = 1000
cr = y - 0.5
ci = x
while :: Double -> Double -> Int -> Int
while zr zi i =
let i' = succ i
temp = zr * zi
zr2 = zr * zr
zi2 = zi * zi
zr' = zr2 - zi2 + cr
zi' = temp + temp + ci in
case (zi2 + zr2 > bailout) of
True -> i'
False -> case (i' > maxIterations) of
True -> 0
False -> while zr' zi' i'
main = do
-- init_time <- System.CPUtime.getCPUTime
let i = map (map (\(x,y) -> mandelbrot (x/40.0) (y/40.0)))
[[(x,y) | x <- [-39..38]] | y <- [-39..38]]
mapM putStr (map (('\n':).(map (\i -> if i==0 then '*' else ' '))) i)
putStr "\n"
-- final_time <- System.CPUtime.getCPUTime
-- let query_time = final_time - init_time
-- printf "Haskell Elapsed %0.2f\n" ((fromIntegral query_time)/10^12::Double)
Cobra 0.8.0
"""
See http://www.timestretch.com/FractalBenchmark.html
History
2009-03-09 Csaba Urbaniczky
2009-03-09 Test in SciTE
With float:: 48-128 ms (in SciTE)
With float & Try Catch &:48-128 ms = same!
With decimal & Try Catch 6.05 - 6.06 s = very much slower!
VB.NET 2005 version on the same computer: 175-180 ms
2009-03-10 Code cleanup by Chuck Esterbrook
2009-04-22 Minor changes by Theo Wollenleben to make it work
with Cobra 0.8.0 for Unix
"""
class Program
def mandelbrot is shared
for y in -39 : 39
print
for x in -39 : 39
if .calc(x/40, y/40), c = '*'
else, c = ' '
print c stop
def calc(ci as number, y as number) as bool is shared
bailOut = 16
maxIterations = 1_000
cr = y - 0.5
zr = 0.0
zi = 0.0
for i in 0 : maxIterations
zr2 = zr * zr
zi2 = zi * zi
if zi2 + zr2 > bailOut, return false
temp = zr * zi
zr = zr2 - zi2 + cr
zi = temp + temp + ci
return true
def main is shared
sw = System.Diagnostics.Stopwatch()
sw.start
.mandelbrot
sw.stop
print
print 'Cobra elapsed: [sw.elapsedMilliseconds] ms'
Erlang R12B5
-module(mandelbrot).
-author("Jim Menard, jimm@io.com").
-export([run/0]).
-define(BAILOUT, 16).
-define(MAX_ITERATIONS, 1000).
%% Idea from http://www.timestretch.com/FractalBenchmark.html
run() ->
io:format("Rendering~n"),
statistics(runtime),
statistics(wall_clock),
lists:map(fun(Y) ->
io:format("~n"),
lists:map(fun(X) -> plot(X, Y) end, lists:seq(-39, 39))
end,
lists:seq(-39, 39)),
io:format("~n"),
{_, Time1} = statistics(runtime),
{_, Time2} = statistics(wall_clock),
Sec1 = Time1 / 1000.0,
Sec2 = Time2 / 1000.0,
io:format("Erlang Elapsed ~p (runtime) ~p (wall clock) seconds~n",
[Sec1, Sec2]).
plot(X, Y) ->
case iterate(X/40.0, Y/40.0) of
0 ->
io:format("*");
_ ->
io:format(" ")
end.
iterate(X, Y) ->
CR = Y - 0.5,
CI = X,
iter_value(CR, CI, 0.0, 0.0, 0.0, 0.0, 0).
iter_value(_CR, _, _ZI, _ZI2, _ZR, _ZR2, I)
when I > ?MAX_ITERATIONS,
is_float(_CR), is_float(_ZI), is_float(_ZI2), is_float(_ZR), is_float(_ZR2) ->
0;
iter_value(_CR, _, _ZI, _ZI2, _ZR, _ZR2, I)
when _ZI2 + _ZR2 > ?BAILOUT,
is_float(_CR), is_float(_ZI), is_float(_ZI2), is_float(_ZR), is_float(_ZR2) ->
I;
iter_value(CR, CI, ZI, ZI2, ZR, ZR2, I)
when
is_float(CR), is_float(ZI), is_float(ZI2), is_float(ZR), is_float(ZR2) ->
Temp = ZR * ZI,
ZRnew = ZR2 - ZI2 + CR,
ZInew = Temp + Temp + CI,
iter_value(CR, CI, ZInew, ZInew * ZInew, ZRnew, ZRnew * ZRnew, I + 1).
Fortran 95 G
! by Erik Wrenholt, ported from C to Fortran 95 by Theo Wollenleben
program main
implicit none
real :: init_time, final_time, query_time
integer, parameter :: BAILOUT = 16
integer, parameter :: MAX_ITERATIONS = 1000
integer :: x, y, i
call cpu_time(init_time)
do y = -39, 38
print *
do x = -39, 38
i = mandelbrot(DBLE(x)/40.0, DBLE(y)/40.0)
if (i==0) then
write(*,'(a)',advance='no') '*'
else
write(*,'(a)',advance='no') ' '
end if
end do
end do
print *
call cpu_time(final_time)
query_time = final_time - init_time
print '("Fortran Elapsed ",f4.2)',query_time
contains
integer function mandelbrot(x, y)
implicit none
double precision, intent(in) :: x, y
double precision :: cr, ci, zi, zr, temp, zr2, zi2
integer :: i
cr = y - 0.5
ci = x
zi = 0.0
zr = 0.0
i = 0
do
i = i + 1
temp = zr * zi
zr2 = zr * zr
zi2 = zi * zi
zr = zr2 - zi2 + cr
zi = temp + temp + ci
if (zi2 + zr2 > BAILOUT) then
mandelbrot = i; exit
end if
if (i > MAX_ITERATIONS) then
mandelbrot = 0; exit
end if
end do
end function mandelbrot
end program main
Pascal FPC 2.2.2
{ fpc -O3 Mandelbrot.pas }
{ by Erik Wrenholt, ported from C to Pascal by Theo Wollenleben }
program Mandelbrot;
uses BaseUnix;
const BAILOUT = 16;
MAX_ITERATIONS = 1000;
var x, y : integer;
i : integer;
buf : tms;
init_time, final_time: clock_t;
function mandelbrot (x, y : double) : integer;
var cr, ci, zi, zr : double;
i : integer;
temp, zr2, zi2 : double;
begin
cr := y - 0.5;
ci := x;
zi := 0.0;
zr := 0.0;
i := 0;
while TRUE do begin
i := i + 1;
temp := zr * zi;
zr2 := zr * zr;
zi2 := zi * zi;
zr := zr2 - zi2 + cr;
zi := temp + temp + ci;
if zi2 + zr2 > BAILOUT then
begin mandelbrot := i; break; end;
if i > MAX_ITERATIONS then
begin mandelbrot := 0; break; end;
end;
end;
begin
FpTimes(buf);
init_time := buf.tms_utime + buf.tms_stime
+ buf.tms_cutime + buf.tms_cstime;
for y := -39 to 38 do begin
writeln;
for x := -39 to 38 do begin
i := mandelbrot(x/40.0, y/40.0);
if i = 0 then
write('*')
else
write(' ');
end;
end;
writeln;
FpTimes(buf);
final_time := buf.tms_utime + buf.tms_stime
+ buf.tms_cutime + buf.tms_cstime;
writeln('Pascal Elapsed ', (final_time-init_time)/100:1:2);
end.
GNU Sather 1.2.3 GCC 4.3.2
-- sacomp -O Mandelbrot.sa -o Mandelbrot
-- by Erik Wrenholt, ported from C to Sather by Theo Wollenleben
class MAIN is
const BAILOUT := 16;
const MAX_ITERATIONS := 1000;
mandelbrot(x:FLTD, y:FLTD):INT is
cr:FLTD := y - 0.5d;
ci:FLTD := x;
zi:FLTD := 0.0d;
zr:FLTD := 0.0d;
i:INT := 0;
loop while!(true);
i := i + 1;
temp:FLTD := zr * zi;
zr2:FLTD := zr * zr;
zi2:FLTD := zi * zi;
zr := zr2 - zi2 + cr;
zi := temp + temp + ci;
if (zi2 + zr2 > BAILOUT.fltd) then
return i;
end;
if (i > MAX_ITERATIONS) then
return 0;
end;
end;
end;
main is
time:TIMES := #;
x,y:INT;
loop y := (-39).upto!(38);
#OUT + "\n";
loop x := (-39).upto!(38);
i:INT := mandelbrot(x.fltd/40.0d, y.fltd/40.0d);
if (i=0) then
#OUT + "*";
else
#OUT + " ";
end;
end;
end;
#OUT + "\n";
elapsed_time:TIMES := time.elapsed;
query_time:INT := elapsed_time.user_time + elapsed_time.sys_time;
-- + elapsed_time.child_user_time + elapsed_time.child_sys_time;
-- seems to give twice the correct time!
#OUT + #FMT("Sather Elapsed %0.2f\n", query_time.fltd/100.0d);
end;
end;
Pike 7.8.116
#! /usr/local/bin/pike
#define BAILOUT 16
#define MAX_ITERATIONS 1000
int mandelbrot (float x, float y) {
float cr = y - 0.5;
float ci = x;
float zi = 0.0;
float zr = 0.0;
int i = 0;
while (1) {
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;
}
}
int main() {
int init_time = gethrvtime();
int x,y;
for (y = -39; y < 39; y++) {
write("\n");
for (x = -39; x < 39; x++) {
int i = mandelbrot(x/40.0, y/40.0);
if (i==0)
write("*");
else
write(" ");
}
}
write("\n");
float query_time = (gethrvtime() - init_time)/1000000.0;
write("Pike Elapsed %0.2f\n", query_time);
return 0;
}
Oz Mozart 1.4.0
% ozc -x Mandelbrot.oz; ./Mandelbrot
% by Erik Wrenholt, ported from C to Oz by Theo Wollenleben
functor
import
Application(exit)
System(printInfo showInfo)
Property
define
BAILOUT = 16.0
MAX_ITERATIONS = 1000
fun {Mandelbrot X Y}
Cr = Y - 0.5
Ci = X
Zi = 0.0
Zr = 0.0
I = 0
fun {Iterate I Zr Zi}
I_ = I + 1
Temp = Zr * Zi
Zr2 = Zr * Zr
Zi2 = Zi * Zi
Zr_ = Zr2 - Zi2 + Cr
Zi_ = Temp + Temp + Ci
in
if Zi2 + Zr2 > BAILOUT then
I_
elseif I_ > MAX_ITERATIONS then
0
else {Iterate I_ Zr_ Zi_} end
end
in
{Iterate I Zr Zi}
end
Init_time Final_time
in
Init_time = {Property.get 'time.user'} + {Property.get 'time.user'}
for Y in ~39..38 do
{System.printInfo "\n"}
for X in ~39..38 do
I = {Mandelbrot {IntToFloat X}/40.0 {IntToFloat Y}/40.0}
in
if I==0 then
{System.printInfo "*"}
else
{System.printInfo " "}
end
end
end
{System.printInfo "\n"}
Final_time = {Property.get 'time.user'} + {Property.get 'time.system'}
{System.printInfo "Oz Elapsed "}
{System.showInfo {IntToFloat (Final_time - Init_time)}/1000.0}
{Application.exit 0}
end
Scheme Guile 1.8.5
#!/usr/bin/guile -s
!#
; by Erik Wrenholt, ported from C to Scheme by Theo Wollenleben
(define BAILOUT 16.0)
(define MAX_ITERATIONS 1000)
(define (mandelbrot x y)
(letrec
((cr (- y 0.5))
(ci x)
(zi 0.0)
(zr 0.0)
(i 0)
(iterate (lambda (i zr zi)
(let*
((i_ (+ i 1))
(temp (* zr zi))
(zr2 (* zr zr))
(zi2 (* zi zi))
(zr_ (+ (- zr2 zi2) cr))
(zi_ (+ temp temp ci)))
(cond
((> (+ zi2 zr2) BAILOUT) i_)
((> i_ MAX_ITERATIONS) 0)
(else (iterate i_ zr_ zi_)))))))
(iterate i zr zi)))
(define init_time 0)
(let ((t (times)))
(set! init_time (+
(tms:utime t) (tms:stime t)
(tms:cutime t) (tms:cstime t))))
(define x 0)(define y -39)
(let for () (if (< y 39) (begin
(newline)
(set! x -39)(let for () (if (< x 39) (begin
(let ((i (mandelbrot (/ x 40.0) (/ y 40.0))))
(if (= i 0)
(display #\*)
(display #\ )))
(set! x (+ x 1))(for))))
(set! y (+ y 1))(for))))
(newline)
(define query_time 0)
(let ((t (times)))
(set! query_time (/ (+
(tms:utime t) (tms:stime t)
(tms:cutime t) (tms:cstime t) (- init_time)) 100.0)))
(display "Scheme Elapsed ")
(display query_time)
(newline)
Scheme 48 1.8
; echo $',set inline-values\n,open posix-time\n,load Mandelbrot.scheme48.scm\n,exit' | scheme48
; by Erik Wrenholt, ported from C to Scheme 48 by Theo Wollenleben
(define BAILOUT 16.0)
(define MAX_ITERATIONS 1000)
(define (mandelbrot x y)
(letrec
((cr (- y 0.5))
(ci x)
(zi 0.0)
(zr 0.0)
(i 0)
(iterate (lambda (i zr zi)
(let*
((i_ (+ i 1))
(temp (* zr zi))
(zr2 (* zr zr))
(zi2 (* zi zi))
(zr_ (+ (- zr2 zi2) cr))
(zi_ (+ temp temp ci)))
(cond
((> (+ zi2 zr2) BAILOUT) i_)
((> i_ MAX_ITERATIONS) 0)
(else (iterate i_ zr_ zi_)))))))
(iterate i zr zi)))
(define init_time
(time-seconds (current-time)))
(define x)(define y)
(set! y -39)(let for () (if (< y 39) (begin
(newline)
(set! x -39)(let for () (if (< x 39) (begin
(let ((i (mandelbrot (/ x 40.0) (/ y 40.0))))
(if (= i 0)
(display #\*)
(display #\ )))
(set! x (+ x 1))(for))))
(set! y (+ y 1))(for))))
(newline)
(define query_time (-
(time-seconds (current-time))
init_time))
(display "Scheme48 Elapsed ")
(display query_time)
(newline)
Mercury 0.13.1 GCC 4.3.2
:- module 'Mandelbrot_mercury'.
% mmc Mandelbrot_mercury.m -o Mandelbrot; ./Mandelbrot
% by Erik Wrenholt, ported from C to Mercury by Theo Wollenleben
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module int, float, time, list, string.
:- func mandelbrot(float, float) = int.
mandelbrot(X, Y) = Return :-
Cr = Y - 0.5,
Ci = X,
Zi = 0.0,
Zr = 0.0,
I = 0,
Return = iterate(I, Zr, Zi, Cr, Ci).
:- func iterate(int, float, float, float, float) = int.
iterate(I, Zr, Zi, Cr, Ci) = Return :-
BAILOUT = 16.0,
MAX_ITERATIONS = 1000,
I_ = I + 1,
Temp = Zr * Zi,
Zr2 = Zr * Zr,
Zi2 = Zi * Zi,
Zr_ = Zr2 - Zi2 + Cr,
Zi_ = Temp + Temp + Ci,
( if Zi2 + Zr2 > BAILOUT then
Return = I_
else if I_ > MAX_ITERATIONS then
Return = 0
else Return = iterate(I_, Zr_, Zi_, Cr, Ci)
).
main(!IO) :-
times(tms(Iutime, Istime, Icutime, Icstime), _, !IO),
Init_time = Iutime + Istime + Icutime + Icstime,
for_y(-39, !IO),
io.nl(!IO),
times(tms(Futime, Fstime, Fcutime, Fcstime), _, !IO),
Query_time = Futime + Fstime + Fcutime + Fcstime - Init_time,
io.format("Mercury Elapsed %0.2f\n",
[f(float(Query_time)/float(clk_tck))], !IO).
:- pred for_y(int::in, io::di, io::uo) is det.
for_y(Y, !IO) :-
if Y < 39 then
io.nl(!IO),
for_x(Y, -39, !IO),
for_y(Y + 1, !IO)
else true.
:- pred for_x(int::in, int::in, io::di, io::uo) is det.
for_x(Y, X, !IO) :-
if X < 39 then
I = mandelbrot(float(X)/40.0, float(Y)/40.0),
( if I = 0 then
io.write_char('*', !IO)
else
io.write_char(' ', !IO)
),
for_x(Y, X + 1, !IO)
else true.
newLISP 10.0.2
#!/usr/bin/newlisp
by Erik Wrenholt, ported from C to newLISP by Theo Wollenleben
!#
(constant 'BAILOUT 16)
(constant 'MAX_ITERATIONS 1000)
(define (mandelbrot x y)
(set 'cr (sub y 0.5))
(set 'ci x)
(set 'zi 0.0)
(set 'zr 0.0)
(set 'i 0)
(catch (while true
(inc i)
(set 'temp (mul zr zi))
(set 'zr2 (mul zr zr))
(set 'zi2 (mul zi zi))
(set 'zr (add (sub zr2 zi2) cr))
(set 'zi (add temp temp ci))
(if (> (add zi2 zr2) BAILOUT)
(throw i))
(if (> i MAX_ITERATIONS)
(throw 0)))))
(set 'query_time (time (begin
(for (y -39 38)
(write-line)
(for (x -39 38)
(set 'i (mandelbrot (div x 40.0) (div y 40.0)))
(if (= i 0)
(print "*")
(print " "))))
(write-line))))
(print (format
"newLisp Elapsed %0.2f\n" (div query_time 1000)))
(exit)
SWI-Prolog 5.6.59
#!/usr/bin/pl -q -O -g main -t halt -f
% by Erik Wrenholt, ported from C to SWI-Prolog by Theo Wollenleben
mandelbrot(X, Y, I_return) :-
Cr is Y - 0.5,
Ci is X,
Zi is 0.0,
Zr is 0.0,
I is 0,
mandelbrot_iterate(I, Zr, Zi, Cr, Ci, I_return).
mandelbrot_iterate(I, Zr, Zi, Cr, Ci, I_return) :-
BAILOUT is 16,
MAX_ITERATIONS is 1000,
I_ is I + 1,
Temp is Zr * Zi,
Zr2 is Zr * Zr,
Zi2 is Zi * Zi,
Zr_ is Zr2 - Zi2 + Cr,
Zi_ is Temp + Temp + Ci,
( Zi2 + Zr2 > BAILOUT ->
I_return is I_;
I_ > MAX_ITERATIONS ->
I_return is 0;
mandelbrot_iterate(I_, Zr_, Zi_, Cr, Ci, I_return)
).
main :-
statistics(runtime, _),
statistics(system_time, _),
for_y(-39),
nl,
statistics(runtime, [_, UTime]),
statistics(system_time, [_, STime]),
Query_time is (UTime + STime)/1000,
format('Prolog Elapsed ~2f', [Query_time]), nl
.
for_y(39) :- !. for_y(Y) :-
nl,
for_x(Y, -39),
Y_ is Y + 1, for_y(Y_).
for_x(_, 39) :- !. for_x(Y, X) :-
mandelbrot(X/40.0, Y/40.0, I),
( I = 0 ->
write('*');
write(' ')
),
X_ is X + 1, for_x(Y, X_).
Eiffel tecomp 0.17
-- tecomp Mandelbrot_tecomp.ace
-- by Erik Wrenholt, ported from C to Eiffel by Theo Wollenleben
-- (found no time procedures for tecomp)
class
MANDELBROT_TECOMP
create
main
feature
BAILOUT: REAL = 16.0
MAX_ITERATIONS: INTEGER = 1000
mandelbrot (x: REAL_64; y: REAL_64): INTEGER
local
cr, ci, zi, zr, temp, zr2, zi2: REAL_64
i: INTEGER
do
cr := y - 0.5
ci := x
zi := 0.0
zr := 0.0
from i := 0 until
zi2 + zr2 > BAILOUT or
i > MAX_ITERATIONS
loop
i := i + 1
temp := zr * zi
zr2 := zr * zr
zi2 := zi * zi
zr := zr2 - zi2 + cr
zi := temp + temp + ci
end
if zi2 + zr2 > BAILOUT then
Result := i
end
if i > MAX_ITERATIONS then
Result := 0
end
end
main
local
x, y, i: INTEGER
do
from y := -39 until y > 38 loop
io.put_new_line
from x := -39 until x > 38 loop
i := mandelbrot(({REAL_64}[x])/40.0, ({REAL_64}[y])/40.0)
if i = 0 then
io.put_character ('*')
else
io.put_character (' ')
end
x := x + 1
end
y := y + 1
end
io.put_new_line
end
end
Gobo Eiffel 3.9
indexing
description: "gec --finalize Mandelbrot.ace"
author: "Erik Wrenholt, ported from C to Gobo Eiffel by Theo Wollenleben"
class
MANDELBROT
inherit
DT_SHARED_SYSTEM_CLOCK
ST_FORMATTING_ROUTINES
create
main
feature
BAILOUT: REAL = 16.0
MAX_ITERATIONS: INTEGER = 1000
mandelbrot (x: REAL_64; y: REAL_64): INTEGER
local
cr, ci, zi, zr, temp, zr2, zi2: REAL_64
i: INTEGER
do
cr := y - 0.5
ci := x
zi := 0.0
zr := 0.0
from i := 0 until
zi2 + zr2 > BAILOUT or
i > MAX_ITERATIONS
loop
i := i + 1
temp := zr * zi
zr2 := zr * zr
zi2 := zi * zi
zr := zr2 - zi2 + cr
zi := temp + temp + ci
end
if zi2 + zr2 > BAILOUT then
Result := i
end
if i > MAX_ITERATIONS then
Result := 0
end
end
main
local
x, y, i: INTEGER
init_time: DT_DATE_TIME
query_time: REAL_64
do
init_time := utc_system_clock.date_time_now
from y := -39 until y > 38 loop
io.put_new_line
from x := -39 until x > 38 loop
i := mandelbrot(({REAL_64}[x])/40.0, ({REAL_64}[y])/40.0)
if i = 0 then
io.put_character ('*')
else
io.put_character (' ')
end
x := x + 1
end
y := y + 1
end
io.put_new_line
query_time := (utc_system_clock.date_time_now - init_time).millisecond_count/1000
print (format ("Eiffel Elapsed $0.2f", <<double_cell (query_time)>>))
io.put_new_line
end
end
Last modification date: 2018-05-17
Theo Wollenleben (alpha0x89 yahoo de)