Solved

Inline functions

Posted on 2002-07-20
3
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 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