Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is there a faster 'inline' possiblity for Class functions?

Reply
Thread Tools

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

 
 
MrThingy
Guest
Posts: n/a
 
      04-28-2005
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.





 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      04-28-2005
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Samee Zahur
Guest
Posts: n/a
 
      04-30-2005
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiuser website - Possiblity of two users clicking submit at sametime Mel ASP .Net 6 08-28-2009 12:42 PM
Inserting Records into SQL Server - is there a faster interface than ADO geskerrett@hotmail.com Python 5 11-14-2005 03:00 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Is there any faster way than dynamic_cast to find the object run time type? steve C++ 17 09-13-2004 08:19 PM
Re:read Dial up is there any we of making it faster Dwayne Taylor Computer Support 6 10-29-2003 07:36 PM



Advertisments