Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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)]"
(* Applescript Version by Erik Wrenholt
I couldn't figure out how to write to stdout
so it buffers the output until the end. *)
on unixTime()
(do shell script "date +%s") as integer
end unixTime
on iterate(x, y)
set BAILOUT to 16
set MAX_ITERATIONS to 1000
set cr to y - 0.5
set ci to x
set zi to 0.0
set zr to 0.0
set i to 0
repeat while i < MAX_ITERATIONS
set i to i + 1
set temp to zr * zi
set zr2 to zr * zr
set zi2 to zi * zi
set zr to zr2 - zi2 + cr
set zi to temp + temp + ci
if zi2 + zr2 > BAILOUT then
return i
end if
end repeat
return 0
end iterate
set t to unixTime()
set mandelbrotString to ""
set nl to (ASCII character 10)
repeat with y from -39 to 38 by 1
set mandelbrotString to mandelbrotString & nl
repeat with x from -39 to 38 by 1
if iterate(x / 40.0, y / 40.0) = 0 then
set mandelbrotString to mandelbrotString & "*"
else
set mandelbrotString to mandelbrotString & " "
end if
end repeat
end repeat
set elapsed to unixTime() - t
mandelbrotString & nl & "Time Elapsed " & elapsed
#!gmd
const int BAILOUT = 16;
const int MAX_ITERATIONS = 1000;
int iterate( 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 = i+1;
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;
}
}
}
import std.stdio;
void mandelbrot(){
//float t = os.time()
float y;
float x;
for( y = -39.0 ; y < 38.0 ; y += 1.0 ) {
for( x = -39.0 ; x < 38.0 ; x += 1.0 ) {
if (iterate(x/40.0, y/40) == 0) {
writef( "*");
} else {
writef( " ");
}
}
writef( "\n");
}
//io.write(string.format("Time Elapsed %d\n", os.time() - t));
}
void main(){
mandelbrot();
}
; 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)))
//import java.util.*;
def iterate(double x, double y)
{
final int BAILOUT = 16;
final int MAX_ITERATIONS = 1000;
double cr = y-0.5f;
double ci = x;
double zi = 0.0f;
double 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;
}
}
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/IoServer
# 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))
#!/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();
#!pike
int BAILOUT = 16;
int MAX_ITERATIONS = 1000;
int iterate( 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 = i+1;
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;
}
}
}
void mandelbrot(){
//float t = os.time()
float y;
float x;
for( y = -39.0 ; y < 38.0 ; y += 1.0 ) {
for( x = -39.0 ; x < 38.0 ; x += 1.0 ) {
if (iterate(x/40.0, y/40) == 0) {
write("*");
} else {
write(" ");
}
}
write("\n");
}
//io.write(string.format("Time Elapsed %d\n", os.time() - t));
}
void main(){
mandelbrot();
}
#!/usr/local/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)
; 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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.