[Shootout-list] Mandelbrot
Greg Buchholz
sleepingsquirrel@member.fsf.org
Mon, 20 Dec 2004 08:48:27 -0800 (PST)
--0-1138617899-1103561307=:18085
Content-Type: text/plain; charset=us-ascii
Content-Id:
Content-Disposition: inline
--- Isaac Gouy <igouy2@yahoo.com> wrote:
> afaict the mandelbrot program spends most time writing the file?
>
One way to reduce time spent writing files it to use another image
file format. Using the *.pgm format (a relative of *.ppm) with "P5" as
the magic number results in a file 1/12th the size.
http://www.die.net/doc/linux/man/man5/pgm.5.html
(You end up writing one byte per pixel, instead of 3 ASCII chars for each
red, green, and blue component = 9 bytes + white space)
And you could really reduce the file size by converting to *.pbm (magic
number "P4") where you only use one bit per pixel.
http://www.die.net/doc/linux/man/man5/pbm.5.html
Those are all pretty simple file formats and you have the advantage of
being able to view them with a wide variety of tools. I guess I'm
partial to having a pretty picture as output, rather than just a table of
numbers. Attached is a C program which spits out a *.pgm file. It's
only about 25% faster on my machine than the full *.ppm version.
Greg Buchholz
__________________________________
Do you Yahoo!?
Yahoo! Mail - now with 250MB free storage. Learn more.
http://info.mail.yahoo.com/mail_250
--0-1138617899-1103561307=:18085
Content-Type: text/x-c; name="f.c"
Content-Description: f.c
Content-Disposition: inline; filename="f.c"
//Mandelbrot benchmark
//compile: gcc -O2 -o fractal fractal.c
//run: fractal 600 >mandel.pgm
//by Greg Buchholz
#include<stdio.h>
int main (int argc, char **argv)
{
int w, h, x, y;
int i, iter = 50;
double limit = 2.0;
double Zr, Zi, Cr, Ci, Tr, Ti;
w = atoi(argv[1]);
h = w;
printf("P5\n%d %d\n255\n",w,h);
for(y=0;y<h;y++)
{
for(x=0;x<w;x++)
{
Zr = 0.0; Zi = 0.0;
Cr = (2*(double)x/w - 1.5); Ci=(2*(double)y/h - 1);
for (i=0;i<iter;i++)
{
Tr = Zr*Zr - Zi*Zi + Cr;
Ti = 2*Zr*Zi + Ci;
Zr = Tr; Zi = Ti;
if (Zr*Zr+Zi*Zi > limit*limit)
break;
}
if(Zr*Zr+Zi*Zi > limit*limit)
putc(255,stdout);
else
putc(0,stdout);
}
}
return(0);
}
--0-1138617899-1103561307=:18085--