Skip to content

Instantly share code, notes, and snippets.

@headius
Created December 11, 2012 05:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save headius/4256104 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/ruby
BAILOUT = 16
MAX_ITERATIONS = 1000
def fractal
puts "Rendering"
y = -39
while y <= 39
puts
x = -39
while x <= 39
i = iterate(x/40.0,y/40.0)
if (i == 0)
print "*"
else
print " "
end
x+=1
end
y+=1
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
i = 0
while i < 10
time = Time.new
fractal
puts
puts "Ruby Elapsed %f" % (Time.new - time)
i+=1
end
Rendering
*
*
*
*
*
***
*****
*****
***
*
*********
*************
***************
*********************
*********************
*******************
*******************
*******************
*******************
***********************
*******************
*******************
*********************
*******************
*******************
*****************
***************
*************
*********
*
***************
***********************
* ************************* *
*****************************
* ******************************* *
*********************************
***********************************
***************************************
*** ***************************************** ***
*************************************************
***********************************************
*********************************************
*********************************************
***********************************************
***********************************************
***************************************************
*************************************************
*************************************************
***************************************************
***************************************************
* *************************************************** *
***** *************************************************** *****
****** *************************************************** ******
******* *************************************************** *******
***********************************************************************
********* *************************************************** *********
****** *************************************************** ******
***** *************************************************** *****
***************************************************
***************************************************
***************************************************
***************************************************
*************************************************
*************************************************
***************************************************
***********************************************
***********************************************
*******************************************
*****************************************
*********************************************
**** ****************** ****************** ****
*** **************** **************** ***
* ************** ************** *
*********** ***********
** ***** ***** **
* * * *
Ruby Elapsed 0.192000
public class bench_fractal extends RObject {
public static void main(String[] args) {
new bench_fractal()._main_();
}
public void _main_() {
RObject $last = RNil;
$last = bench_fractal.BAILOUT = new RFixnum(16L);
$last = bench_fractal.MAX_ITERATIONS = new RFixnum(1000L);
RObject i = RNil;
$last = i = new RFixnum(0L);
while (i.$less(new RFixnum(10L)).toBoolean()) {
RObject time = RNil;
$last = time = new RTime();
$last = this.fractal();
$last = this.puts();
$last = this.puts(new RString("Ruby Elapsed %f")
.$percent(new RTime().$minus(time)));
$last = i = i.$plus(new RFixnum(1L));
;
}
$last = RNil;
;
}
public static RObject BAILOUT = RNil;
public static RObject MAX_ITERATIONS = RNil;
public RObject fractal() {
RObject $last = RNil;
$last = this.puts(new RString("Rendering"));
RObject y = RNil;
$last = y = new RFixnum(-39L);
while (y.$less$equal(new RFixnum(39L)).toBoolean()) {
$last = this.puts();
RObject x = RNil;
$last = x = new RFixnum(-39L);
while (x.$less$equal(new RFixnum(39L)).toBoolean()) {
RObject i = RNil;
$last = i = this.iterate(x.$div(new RFloat(40.0)), y
.$div(new RFloat(40.0)));
if (i.$equal$equal(new RFixnum(0L)).toBoolean()) {
$last = this.print(new RString("*"));
} else {
$last = this.print(new RString(" "));
}
$last = RNil;
$last = x = x.$plus(new RFixnum(1L));
;
}
$last = RNil;
$last = y = y.$plus(new RFixnum(1L));
;
}
$last = RNil;
;
return $last;
}
public RObject iterate(RObject x, RObject y) {
RObject $last = RNil;
RObject cr = RNil;
$last = cr = y.$minus(new RFloat(0.5));
RObject ci = RNil;
$last = ci = x;
RObject zi = RNil;
$last = zi = new RFloat(0.0);
RObject zr = RNil;
$last = zr = new RFloat(0.0);
RObject i = RNil;
$last = i = new RFixnum(0L);
while (new RFixnum(1L).toBoolean()) {
$last = i = i.$plus(new RFixnum(1L));
RObject temp = RNil;
$last = temp = zr.$times(zi);
RObject zr2 = RNil;
$last = zr2 = zr.$times(zr);
RObject zi2 = RNil;
$last = zi2 = zi.$times(zi);
$last = zr = zr2.$minus(zi2).$plus(cr);
$last = zi = temp.$plus(temp).$plus(ci);
if (zi2.$plus(zr2).$greater(bench_fractal.BAILOUT).toBoolean()) {
$last = i;
return $last;
}
$last = RNil;
if (i.$greater(bench_fractal.MAX_ITERATIONS).toBoolean()) {
$last = new RFixnum(0L);
return $last;
}
$last = RNil;
;
}
$last = RNil;
;
return $last;
}
}
Rendering
*
*
*
*
*
***
*****
*****
***
*
*********
*************
***************
*********************
*********************
*******************
*******************
*******************
*******************
***********************
*******************
*******************
*********************
*******************
*******************
*****************
***************
*************
*********
*
***************
***********************
* ************************* *
*****************************
* ******************************* *
*********************************
***********************************
***************************************
*** ***************************************** ***
*************************************************
***********************************************
*********************************************
*********************************************
***********************************************
***********************************************
***************************************************
*************************************************
*************************************************
***************************************************
***************************************************
* *************************************************** *
***** *************************************************** *****
****** *************************************************** ******
******* *************************************************** *******
***********************************************************************
********* *************************************************** *********
****** *************************************************** ******
***** *************************************************** *****
***************************************************
***************************************************
***************************************************
***************************************************
*************************************************
*************************************************
***************************************************
***********************************************
***********************************************
*******************************************
*****************************************
*********************************************
**** ****************** ****************** ****
*** **************** **************** ***
* ************** ************** *
*********** ***********
** ***** ***** **
* * * *
Ruby Elapsed 0.040000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment