Solved

Inline functions

Posted on 2002-07-20
3
283 Views
Last Modified: 2013-11-20
Is there some kind of a problem with inline functions when using MFC?

I have written a class which manages arrays and it has several inline functions. For example, the operator [] is inline.

First of all, if the header and the body of a function are in different files and the function is declared inline, the program does not link! If I put the body inside the class declaration (which is supposed to make it automatically inline), the program links. However, there is no difference from the not-inline function. I measured times and realized that a simple array (straightforward memory usage) works far more quickly than a class which has a simple array as its data member and uses inline [] operator!

Am I doing something wrong? If so, please point it out. If not, could someone explain to me, what is the point of writing a class for managing arrays when it wastes tons of time?
0
Comment
Question by:Lescha
  • 2
3 Comments
 
LVL 8

Expert Comment

by:fl0yd
ID: 7167677
Whenever using inline functions the function has to reside in the header file. It doesn't matter whether you use automatic inlining or define the function after the class definition. A violation to this rule will result in code that doesn't compile, no matter what compiler you are using.

When you say "there is no difference" did you look at the produced code? I doubt that you can measure a difference in time unless actually measuring the clock cycles with rdtsc. If you use the 'inline'-keyword with msvc it is not guaranteed to be inlined. Msvc decides whether inlining does improve the speed of code or not, no matter if you specified an inline-keyword or not. It usually beats the developer at deciding properly. If you are *ABSOLUTELY SURE* that you need to inline a function you can use __forceinline to override the compiler's decision.

I'm not sure what exactly you compared to find out that an array is far more quickly than a class containing an array. First of all, object oriented programming wasn't invented for speed, but rather safety and managability of code. Obviously, if there is more code to execute it will most likely not be faster. But...

#define DIMENSION 100

class array_class {
    unsigned char _array[DIMENSION];
public:
    unsigned char& operator [] ( int index ) { return _array[index]; }
} A;

unsigned char a = A[10];

shouldn't be noticably slower than

unsigned char B[DIMENSION];
unsigned char b = B[10];

if it is slower at all.
0
 
LVL 1

Author Comment

by:Lescha
ID: 7168138
Yes, well, the problem is I work with dimensions on the scale of 1000000 rather than 100...

As for how I measured: with GetCountTick or something like that (returns elapsed ms). And I didn't even need the function to measure, it is _noticeably_ slower!
0
 
LVL 8

Accepted Solution

by:
fl0yd earned 75 total points
ID: 7168372
Did you try my source with millions? There should be no difference in release build. If you want to be sure you will have to look at the compiler output -- turn on assembly listing generation for the files in question and you will see much clearer what's going on.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Login Script to Copy Folders 12 35
Looking for a specific application/software 2 109
iSeries DB2 Query 2 95
Line meaning 9 85
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question