Solved

Inline functions

Posted on 2002-07-20
3
286 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

728 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