Create your blog and photo album with postbit
Create your blog and photo album

Create new post

Content:

Upload a picture:
Tags (keywords separated by comma)

Save Cancel
Grupo Fractarte fractarte:   Followers: 10 ; Following: 2

Explore Grupo Fractarte's photo albums:
Sand Icons (12)
Misc Fractals (6)
Geometric art (4)
3D (2)
Mandelbrot Set Gallery (1)
Revista Brasileira - ABL (12)

Programa em PERL para gerar o Conjunto de Mandelbrot



Esta imagem do fractal Conjunto de Mandelbrot foi gerada pelo programa em PERL (abaixo), rodado com o seguinte parâmetro:
perl mandelbrot.pl 300 > imagem.pbm
O parâmetro 300 significa que a imagem foi gerada com 300x300 pixels.
O arquivo gerado "imagem.pbm" foi convertido para gif para ser adicionado nesta página.

Esta versão simples e com pouca otimização roda 16% mais rápido do que a implementação anterior em PERL do site de comparação de velocidade de linguagens (shootout.alioth.debian.org - Computer Language Benchmarks). Veja mais abaixo a versão otimizada (mais complexa) e que usa threads para ficar mais rápido ainda.

# The Computer Language Shootout
# implemented by Rodrigo de Almeida Siqueira

use strict;

my $ITER = 50;
my $LIMITSQR = 4;
my $xmin = -1.5;
my $xmax = 0.5;
my $ymin = -1;
my $ymax = 1;

my ($w, $h, $Cr, $Ci, $Zr, $Zi, $Tr, $Ti);

$w = $h = shift || 80;

print "P4\n$w $h\n";

for my $y (0..$h-1) {

my @v;

my $Ci = 2 * $y / $h + $ymin;

for my $x (0..$w-1) {

my $Zr = $Zi = $Tr = $Ti = 0;
my $Cr = 2 * $x / $h + $xmin;

my $in_m = 1;

for (1..$ITER) {

$Zi = 2 * $Zr * $Zi + $Ci;
$Zr = $Tr - $Ti + $Cr;
$Ti = $Zi * $Zi;
$Tr = $Zr * $Zr;

if (($Tr + $Ti) > $LIMITSQR) {
$in_m = 0;
last;
}

}

push @v, $in_m;
}

print pack 'B*', pack 'C*', @v;

}

### END ###


Versão otimizada (30% mais rápido do que o algoritmo original) e também paralelizada para funcionar com múltiplos processadores (cores):

# The Computer Language Benchmarks Game
# http://shootout.alioth.debian.org/
#
# contributed by Rodrigo de Almeida Siqueira
# parallelised by Philip Boulain

use strict;
use threads;

my $threads = 6; # Workers; ideally slightly overshoots number of processors

my $MAXITER = 50;
my $LIMIT = 4;
my $xmin = -1.5;
my $ymin = -1;

my ($w, $h);

$w = $h = shift || 200;
my $invN = 2/$w;

print "P4\n$w $h\n"; # PBM image header

sub lines {

my $y1 = shift;
my $y2 = shift;
my @v;

my $checknext=1;

for my $y ($y1..$y2) {

my $Ci = $y * $invN + $ymin;

X:
for my $x (0..$w-1) {

my ($Zr, $Zi, $Tr, $Ti);

my $Cr = $x * $invN + $xmin;

if ($checknext) {

# Iterate with checking (likely outer pixel)
for (1..$MAXITER) {
$Zi = 2 * $Zr * $Zi + $Ci;
$Zr = $Tr - $Ti + $Cr;
$Ti = $Zi * $Zi;
$Tr = $Zr * $Zr;

if ($Tr + $Ti > $LIMIT) {
push @v, 0; # White pixel
next X;
}
}

push @v, 1; # Black pixel
$checknext = 0; # Is inner pixel.

} else {

# Iterate without checking (likely inner pixel)

for (1..$MAXITER) {
$Zi = 2 * $Zr * $Zi + $Ci;
$Zr = $Tr - $Ti + $Cr;
$Ti = $Zi * $Zi;
$Tr = $Zr * $Zr;
}

if ($Tr+$Ti <= 4) {
push @v, 1;
} else { # $Tr+$Ti is a large number or overflow ('nan' or 'inf')
push @v, 0;
$checknext = 1;
}

}

}

}
return pack 'B*', pack 'C*', @v;

}

use constant MAXPIXEL => 524288; # Maximum pixel buffer per thread
my $each = int($h / $threads);
$each = int(MAXPIXEL / $w) if ($each * $w) > MAXPIXEL;
$each = 1 if $each < 1;
$|=1;

my $y = 0;
my @workers;
# Work as long as we have lines to spawn for or threads to collect from
while(@workers or ($y < $h)) {
# Create workers up to requirement
while((@workers < $threads) and ($y < $h)) {
my $y2 = $y + $each;
$y2 = $h if $y2 > $h;
push(@workers, threads->create('lines', $y, $y2-1));
$y = $y2;
}
# Block for result from the leading thread (to keep output in order)
my $next = shift @workers;
print $next->join();
}


Versão em linguagem LUA:
http://shootout.alioth.debian.org/u32/benchmark.php?test=mandelbrot&lang=lua

Versão em PERL no servidor de language benchmark performance:
http://shootout.alioth.debian.org/u32/program.php?test=mandelbrot&lang=perl&id=3

Versão em FORTRAN (muito rápida):
http://shootout.alioth.debian.org/u32/program.php?test=mandelbrot&lang=ifc&id=6

Versão em Java (mandelbrot.java):
http://code.google.com/p/core-java-performance-examples/source/browse/trunk/src/test/java/com/google/code/java/core/shootout/

Post by Grupo Fractarte (2012-01-16 19:24)

Post your comment:

Name: Email: Site:




| Explore users | New posts | Create your blog | Create your photo album |
| About Postbit | Our blog | Terms of use | Contact Postbit |


Copyright © 2019 - postbit.com