Skip to content

Commit

Permalink
Add first Complex benchmark, rc-mandelbrot
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoffrey Broadwell committed Jul 5, 2014
1 parent b36e80b commit 7c5a914
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
10 changes: 10 additions & 0 deletions minibenchmarks.pl
Expand Up @@ -61,4 +61,14 @@
perl6 => [qw( BENCH/perl6/rc-9-billion-names SCALE )],
nqp => [qw( BENCH/nqp/rc-9-billion-names SCALE )],
},
{
name => 'rc-mandelbrot',
skip => [qw( )],
tags => [qw( complex io output )],
scale => 1 << 5,
work => sub { $_[0] * $_[0] * 2 / 3 },
perl5 => [qw( BENCH/perl5/rc-mandelbrot SCALE )],
perl6 => [qw( BENCH/perl6/rc-mandelbrot SCALE )],
nqp => [qw( BENCH/nqp/rc-mandelbrot SCALE )],
},
]
45 changes: 45 additions & 0 deletions nqp/rc-mandelbrot
@@ -0,0 +1,45 @@
my $SQUISH := 1.5;

sub MAIN($prog, $w = 31, $max_iterations = 50) {
my $h := nqp::floor_n($w / $SQUISH + 0.5);

my $re_min := -2;
my $re_max := 1/2;
my $im_min := -5/4;
my $im_max := 5/4;

# Allow SCALE == 0 for compile time testing
nqp::exit(0) if $w < 2;

my $re_step := ($re_max - $re_min) / ($w - 1);
my $im_step := ($im_max - $im_min) / ($h - 1);

my @color_map := < . , ; * $ # @ >;
@color_map.unshift(' ');

my sub mandelbrot($re, $im) {
my $z_re := $re;
my $z_im := $im;
my $i := 1;
while $i <= $max_iterations {
my $tmp := $z_re * $z_re - $z_im * $z_im + $re;
$z_im := 2 * $z_re * $z_im + $im;
$z_re := $tmp;
return $i if $z_re * $z_re + $z_im * $z_im > 4;
$i++;
}
0;
}

my $y := $im_max;
while $y >= $im_min {
my $x := $re_min;
while $x <= $re_max {
my $iter := mandelbrot($x, $y);
print(@color_map[$iter % @color_map]);
$x := $x + $re_step;
}
print("\n");
$y := $y - $im_step;
}
}
33 changes: 33 additions & 0 deletions perl5/rc-mandelbrot
@@ -0,0 +1,33 @@
use 5.010;
use Math::Complex;
use constant SQUISH => 1.5;

my $w = $ARGV[0] // 31;
my $h = int($w / SQUISH + .5);
my $max_iterations = $ARGV[1] // 50;
my ($re_min, $re_max) = (-2, 1/2);
my ($im_min, $im_max) = (-5/4, 5/4);

# Allow SCALE == 0 for compile time testing
exit(0) if $w < 2;

my $re_step = ($re_max - $re_min) / ($w - 1);
my $im_step = ($im_max - $im_min) / ($h - 1);

my @color_map = (' ', qw( . , ; * $ # @ ));

sub mandelbrot {
my ($z, $c) = @_[0,0];
for (1 .. $max_iterations) {
$z = $z * $z + $c;
return $_ if abs $z > 2;
}
}

for (my $y = $im_max; $y >= $im_min; $y -= $im_step) {
for (my $x = $re_min; $x <= $re_max; $x += $re_step) {
my $iter = mandelbrot($x + $y * i);
print $color_map[$iter % @color_map];
}
print "\n"
}
33 changes: 33 additions & 0 deletions perl6/rc-mandelbrot
@@ -0,0 +1,33 @@
constant SQUISH = 1.5;

sub MAIN($w = 31, $max_iterations = 50) {
my $h = round($w / SQUISH);

my ($re_min, $re_max) = (-2, 1/2);
my ($im_min, $im_max) = (-5/4, 5/4);

my $re_step = ($re_max - $re_min) / ($w - 1);
my $im_step = ($im_max - $im_min) / ($h - 1);

# Allow SCALE == 0 for compile time testing
exit(0) if $w < 2;

my @color_map = ' ', < . , ; * $ # @ >;

my sub mandelbrot($c) {
my $z = $c;
for 1 .. $max_iterations {
$z = $z * $z + $c;
return $_ if abs($z) > 2;
}
0;
}

loop (my $y = $im_max; $y >= $im_min; $y -= $im_step) {
loop (my $x = $re_min; $x <= $re_max; $x += $re_step) {
my $iter = mandelbrot($x + $y * i);
print @color_map[$iter % @color_map];
}
print "\n"
}
}

0 comments on commit 7c5a914

Please sign in to comment.