Is there a faster 'inline' possiblity for Class functions?

Discussion in 'C++' started by MrThingy, Apr 28, 2005.

  1. MrThingy

    MrThingy Guest

    How can I have a function within a class that I know is going to be
    called almost thousands of times during a loop cycle and have it run as
    fast as possible, without using bug-prone #define macros?

    I've read some information that says that ordinary Class member
    functions running on private data tend to be optimised by the compiler
    anyway?

    -------------

    Long boring bit: I'm currently converting an old program which has a
    global array of the form:-

    int *heightMap; // standard heightfield for landscape

    which is allocated size:-

    heightMap = new int[width * height]; // where "width" and "height" are
    stated using #defines.

    I used to access this array using a cheap and nasty #define function.

    #define indexMap(x, y) ((x)&(width-1) + ((y)&(height-1)) * width) // yuck!

    I'm now rewriting the code using sensible and easy to read classes, but
    I'd like to be able to call something like:

    current_height = Landscape.indexMap(x, y);

    int Landscape::indexMap(int x, int y) {
    return ((x)&(width-1) + ((y)&(height-1)) * width)
    }

    lots of times during the main program loop.
    MrThingy, Apr 28, 2005
    #1
    1. Advertising

  2. MrThingy wrote:
    >
    > How can I have a function within a class that I know is going to be
    > called almost thousands of times during a loop cycle and have it run as
    > fast as possible, without using bug-prone #define macros?


    Exactly as you did it: Create an inline function.
    Your function is simple enough that most compilers will optimize
    it by inlining the code directly at the callers side.

    class Landscape
    {
    public:
    ....

    int indexMap(int x, int y) {
    return ((x)&(width-1) + ((y)&(height-1)) * width)
    }
    ....
    };

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Apr 28, 2005
    #2
    1. Advertising

  3. MrThingy

    Samee Zahur Guest

    > I've read some information that says that ordinary Class member
    > functions running on private data tend to be optimised by the

    compiler
    > anyway?

    Exactly, and in a function as simple as this, you shouldn't have to
    worry about inlining it yourself.

    And since you are converting from #defines to consts anyway, you
    might as well consider declaring width and height as consts as well -
    they let you tuck constants inside namespaces/class scopes. Avoids
    naming conflicts.

    Samee
    Samee Zahur, Apr 30, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    457
  2. Daniel Vallstrom
    Replies:
    2
    Views:
    1,876
    Kevin Bracey
    Nov 21, 2003
  3. Rahul
    Replies:
    3
    Views:
    454
    James Kanze
    Feb 28, 2008
  4. Mel
    Replies:
    6
    Views:
    437
    MLightsOut
    Aug 28, 2009
  5. Good Guy
    Replies:
    0
    Views:
    152
    Good Guy
    Jun 28, 2013
Loading...

Share This Page