help w/optimization

This is a discussion on help w/optimization within the C++ Programming forums, part of the General Programming Boards category; My program generates a lot of coordinates, over 3 million at 10 iterations of the rendering loop. my class looks ...

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    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!
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User nepper271's Avatar
    Join Date
    Jan 2008
    Location
    Brazil
    Posts
    50
    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

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    Quote Originally Posted by dudeomanodude View Post
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21