Overhead of Inline Functions

Mar 14, 2011 at 4:41pm
I am writing a class to log string messages sent to it. To increase performance, I may at some point want to disable logging at compile time. I intend to make all the functions of the log class inline and surround their code with conditional directives as follows.

1
2
3
4
5
6
7
8
9
10
11
class Log {
   // etc
public:
   // etc
   void WriteMessage(std::string msg)
   {
   #ifndef NOLOG
      // all the function code
   #endif
   }
};


Therefore, by defining the macro NOLOG, I will make the function WriteMessage inline and empty.

EDITED: I know there is no overhead for inline functions due to passing parameters. Does that mean that an empty inline function result in no processor instructions whatsoever in the final executable?

Thanks in advance for any help.
Last edited on Mar 14, 2011 at 4:45pm
Mar 14, 2011 at 4:47pm
The compiler is what actually makes the decision, a compiler may recognise that the function does nothing and ignore it or actually generate code for it, that may happen whether the function was declared inline or not
Mar 14, 2011 at 4:51pm
Can I check whether my compiler (I use MSVC2010 express) will do this? Is there an option to force it one way or the other?
Mar 14, 2011 at 4:54pm
Just look at the disassembly.
Mar 14, 2011 at 4:57pm
Can I check whether my compiler (I use MSVC2010 express) will do this?

You could compare the executable size with when the functions aren't inline. Or call microsoft.

Is there an option to force it one way or the other?

There may be a compiler option for your specific compiler to enforce a specific behaviour, but in general you should just let the compiler do its job.
Mar 14, 2011 at 5:17pm
I ran some tests in VC++ and whenever the function was empty, inline or not, the file size was exactly the same as when the function wasn't included in the file at all. Thus it looks like I can just get on with it and trust the compiler with optimisation as you said :)

Thanks everyone.
Mar 14, 2011 at 5:21pm
Incidentally, don't indent your #ifdefs. They're not part of the program logic they're being indented under.
Mar 14, 2011 at 5:35pm
I don't necessarily agree with you filipe. I think it help to improve clarity in this case.
Mar 14, 2011 at 6:29pm
I've been reading a lot of CPython, some MRI (Ruby) and taking peeks at GCC, and they all use this convention, so maybe I'm just too used to it.
Mar 14, 2011 at 6:53pm
Well when I actually wrote the code in the Visual C++ IDE, I didn't indent them, partially because of what you said, and partially because that's how the IDE arranges it :P

But when I typed it up on here, I thought I'd indent it since I try to make everything as neat as I possibly can before I post ;)
Last edited on Mar 14, 2011 at 10:56pm
Topic archived. No new replies allowed.