473,322 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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.

Jul 23 '05 #1
2 1139
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
kb******@gascad.at
Jul 23 '05 #2
> 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

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jeff Williams | last post by:
The common method of defining template classes and functions is to put the definition and declaration into the same header file. Or at least I believe it to be the common method and it is...
13
by: A | last post by:
Hi, I'm having problems completing a project in C++. I have been using inline functions in some of my header files. I have only done so for simple functions that only have 1 statement (eg....
23
by: YinTat | last post by:
Hi, I learned C++ recently and I made a string class. A code example is this: class CString { public: inline CString(const char *rhs) { m_size = strlen(rhs);
17
by: steve | last post by:
I am writing for game application. Performance is an issue. Any advise would be appreiciated.
7
by: Srini | last post by:
Hello, Rules for inline functions say that they have to be defined in the same compilation unit as their declarations. For class member functions this means that the inline member functions must...
43
by: Patrick Laurent | last post by:
Hello I have a program with many many inlined template functions It is essential for the execution speed that every (or almost every) function marked as inlined, becomes really inlined by the...
6
by: John Ratliff | last post by:
I was reading the C++ FAQ Lite about inline functions, and it says the following (http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.7) " It's usually imperative that the...
6
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual...
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.