Need Help speeding up mandelbrot algo

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");
}
}
 
E

Eric Sosman

Chris said:
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.

#include <stdio.h>
#include <math.h>

typedef struct complex Complex;
struct complex{ double real; double imag;};
[...]
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){
^^^^^^

Ancient time-saving trick: It's usually quicker to
compare a distance to the square of a radius than to
compare the square root of the distance to the radius.

In some circumstances, it may also pay to compare
the x and y components to the radius individually,
possibly avoiding the need for the multiply-and-add.
 
B

Barry Schwarz

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)

snip rest of functions

You might want to consider passing pointers to the structures instead
of the structures themselves. On a lot of systems, doubles are 8
bytes and pointers are 4. Seems like you would save 75% on each
structure in question.


<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top