# help w/optimization

• 02-08-2008
dudeomanodude
help w/optimization
My program generates a lot of coordinates, over 3 million at 10 iterations of the rendering loop.

my class looks something like this:
Code:

```class Coord{     public:         Coord(double x, double y):xcoord(x),ycoord(y), theta( calcTheta() ){};         inline double calcTheta();     private:         double xcoord;         double ycoord;         double theta; }; inline double Coord::calcTheta(){     double t;     // Perform floating-point calculations to obtain theta...     return t; }```
I'm just wondering what effect does 'inline' have here? Is it doing anything better at all? Is the idea of calling a function in the initialization list a good idea?

Any suggestions are welcome!
• 02-08-2008
matsp
With modern compilers, inline will probably make no difference at all. If the compiler thinks it's a good idea to inline the function, then the compiler will do so. If it doesn't think it's a good idea, you won't get it inlined - this depends on the size of the function, essentially.

There may be ways that you can "force inline" a function the compiler doesn't like to inline - but most of the time, this will give little or no actual benefit to execution speed.

--
Mats
• 02-08-2008
nepper271
Maybe you should try to optimize your calculation of theta. I don't know what you are doing, but if you're using, for instance, arctan to calculate it, you could try a series expansion of the function arctan. I found this
http://en.wikipedia.org/wiki/Taylor_series
and made a simple implementation of the arctan calculation.
Code:

```double arcotan(double x, int n) {   double soma = x, parc = x;     for (int i = 1; i < n; i++)     {       parc = -parc*x*x*(2*i-1)/(2*i+1);       soma += parc;     }   return soma; } int main() {   for (int i = 1; i < 10; i++)     {       cout << "n = " << i << endl;       for (double j = -0.9; j <= 0.9; j+= 0.1)         {           cout << "x = " << j << " : " << arcotan(j,i) - atan(j) << endl;         }     }     return 0; }```
Unfortunately, this expansion works only for x between -1 and 1, and the error between the function and the series increases as x reaches this boundary. Of course that there are other kinds of expansion, and I remember seeing one for arctan that was much more useful. But I don't remember it right now, sorry.
Maybe you can find something in the book 'Numerical Recipes in C++'.

Nepper271
• 02-08-2008
iMalc
Quote:

Originally Posted by dudeomanodude
My program generates a lot of coordinates, over 3 million at 10 iterations of the rendering loop.

I'm just wondering what effect does 'inline' have here? Is it doing anything better at all? Is the idea of calling a function in the initialization list a good idea?

'inline' makes the least performance impact of anything you could possibly do. In fact it could even make it worse. The best option is for you to post some of the actual code that does the calculations. Make sure it is a direct copy-and paste too, not some stripped down code.
There are plenty of us on here that are good at optimisation and may well speed it up by something like 50% or something. Inline probably wont make 0.01% difference.