LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-03-2011, 03:32 PM   #1
gothrog
Member
 
Registered: Jun 2004
Distribution: Yellow Dog, Fedora, RedHat, Centos, Ubuntu, Suse Linux
Posts: 106

Rep: Reputation: 15
When to use inline C++


Hi All,

Is it good to set a recursive function as inline?

I would think it would increase the performance speed of the program.
 
Old 02-03-2011, 05:44 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
inline is just a hint. It's ignored for recursive functions and functions that are too large.
 
Old 02-03-2011, 07:12 PM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by tuxdev View Post
inline is just a hint.
It is also an exception to the "one definition rule".

If you want to define a function in a header file that is included in multiple cpp files, the function must be inline to avoid violating the "one definition rule".

Quote:
It's ignored for recursive functions and functions that are too large.
I've mainly looked at that topic in ICC, rather than GCC. Except for that ODR exception, ICC normally ignores almost anything the programmer might try to influence whether a function is inlined or not. ICC just decides for itself what is "best" in each case. Often those are terrible decisions.

A recursive function can be inlined and ICC often does. Inlining a function does not imply that everything it calls gets inlined. Obviously you can't inline everything a recursive function calls. It is sometimes effective to inline a function without inlining everything it calls. That is especially true of recursive functions. ICC does a pretty good job of inlining one or more levels of a recursive function. I don't know how (or how well) GCC handles that.

ICC's worst inlining decisions seem to apply the same top down logic (needed to inline a few levels of a recursive function) but apply them to more common non recursive situations where it is a terrible idea. Inlining in most cases would be much smarter bottom up. GCC does better than ICC in specific cases where ICC inlining decisions are especially stupid. But that is a very biased sample set. I know much less about GCC in more general cases.

Quote:
Originally Posted by gothrog View Post
Is it good to set a recursive function as inline?

I would think it would increase the performance speed of the program.
A small recursive function usually runs a lot faster because ICC inlines a few levels of it into itself. In ICC, that depends only on which optimizations are enabled. The inline keyword in that situation has no effect on whether (or how much) the function is inlined.

I expect GCC can also speed up a small recursive function with inlining. But I don't know how to predict the details short of trying it and looking at a disassembly of the results.

Last edited by johnsfine; 02-03-2011 at 07:17 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Snort Inline priyadarshan Linux - Security 4 04-08-2009 09:25 AM
INLINE Functions sdow Programming 3 10-02-2008 02:35 PM
C++ inline - where to specify? mfurnanz Programming 3 01-17-2007 03:29 PM
Snort inline enyawix Linux - Networking 0 09-24-2004 03:10 PM
inline AutoComplete hmorrow General 2 06-20-2001 04:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration