C
Chris
Below is my code for the mandelbrot set. This takes a long time to run
about a minute and a 1/2. I was wondering if anyone had any ideas on
improving the pixval function as this where most of the time is taken.
thanks.
#include <stdio.h>
#include <math.h>
typedef struct complex Complex;
struct complex{ double real; double imag;};
#define size 1000
int pix[size][size];
Complex mult( const Complex a, const Complex b)
{
Complex res;
res.real = a.real*b.real-a.imag*b.imag;
res.imag = a.real*b.imag+a.imag*b.real;
return res;
}
Complex add( const Complex a, const Complex b)
{
Complex res;
res.real = a.real+b.real;
res.imag = a.imag+b.imag;
return res;
}
double abs( const Complex a)
{
return sqrt( a.real*a.real + a.imag*a.imag);
}
int pixval( double xx, double yy)
{
Complex z = {0,0};
Complex c ;
int j;
c.real = yy; c.imag = xx;
for (j = 0 ; j < 1000 && abs(z) < 2 ; j+=1){
z = add( mult(z,z), c);
}
return j / 20;
}
void fillpix(double x0, double y0, double xinc, double yinc)
{
int x, y;
for( x = 0 ; x < size ; ++x)
for( y = 0 ; y < size ; ++y)
pix[x][y] = pixval( x0+x*xinc, y0+y*yinc);
}
int main()
{
int x,y;
fillpix(-2.0 , -2.0 , 4.0/size, 4.0/size);
for (y = 0 ; y < size; y+=(size/20)){
for (x = 0 ; x < size; x+=(size/50))
printf("%c", ' '+pix[y][x]);
printf("\n");
}
}
about a minute and a 1/2. I was wondering if anyone had any ideas on
improving the pixval function as this where most of the time is taken.
thanks.
#include <stdio.h>
#include <math.h>
typedef struct complex Complex;
struct complex{ double real; double imag;};
#define size 1000
int pix[size][size];
Complex mult( const Complex a, const Complex b)
{
Complex res;
res.real = a.real*b.real-a.imag*b.imag;
res.imag = a.real*b.imag+a.imag*b.real;
return res;
}
Complex add( const Complex a, const Complex b)
{
Complex res;
res.real = a.real+b.real;
res.imag = a.imag+b.imag;
return res;
}
double abs( const Complex a)
{
return sqrt( a.real*a.real + a.imag*a.imag);
}
int pixval( double xx, double yy)
{
Complex z = {0,0};
Complex c ;
int j;
c.real = yy; c.imag = xx;
for (j = 0 ; j < 1000 && abs(z) < 2 ; j+=1){
z = add( mult(z,z), c);
}
return j / 20;
}
void fillpix(double x0, double y0, double xinc, double yinc)
{
int x, y;
for( x = 0 ; x < size ; ++x)
for( y = 0 ; y < size ; ++y)
pix[x][y] = pixval( x0+x*xinc, y0+y*yinc);
}
int main()
{
int x,y;
fillpix(-2.0 , -2.0 , 4.0/size, 4.0/size);
for (y = 0 ; y < size; y+=(size/20)){
for (x = 0 ; x < size; x+=(size/50))
printf("%c", ' '+pix[y][x]);
printf("\n");
}
}