- Speed comparison of member function and global function
- Posted by Timo Kruse on January 12th, 2005
Hi.
I was playing around with some different ways of designing functions and
measuring the speed of the execution. I am working on VC++ 6.0 and running
on WindowsXP. I use release debug optimized for speed. The function i am
trying to do is just a simple add of integers and i repeat this 1 billion
times, measuring the time before and after execution with timeGetTime().
I got a result that i cannot understand. A global function is very slow
(6000ms), while a inline function is considerably faster (800ms). Now i made
a class and tried a inline memberfunction of this class, the result was also
800ms. So far it was as expected. Removing the inline and recompiling was
showing that the memberfunction (without inline) is also running at 800 ms.
How can that be? What is different in the way a global and a memberfunction
are treated internally, so that the speed of a memberfunction is that much
better?
I would appreciate any comments or hints to where i can find some reading
material.
Thanks,
Timo
- Posted by Frank Adam on January 12th, 2005
On Wed, 12 Jan 2005 12:41:30 +0530, "Timo Kruse"
<timo.(nospam)kruse@gmx.de> wrote:
ie:
class foo{
public:
long Calc() { blabla++; }
}
If so, the member is treated as inline.
--
Regards, Frank
- Posted by Timo Kruse on January 12th, 2005
"Frank Adam" <fajp@notthis.optushome.com.au> wrote in message
news:h0m9u0tm0d0pvv90dghluoehlbifpr1i1v@4ax.com...
Thanks. I defined it inside, just as you expected.
- Posted by Vince Morgan on January 12th, 2005
I read recently that many modern compilers may inline a function in a class,
even if it is defined outside of the header, if the compiler determines it
to be an optimization.
I'm interested to know more about this myself . At times past I have
laboured over whether to, or not to, inline a function.
I'm certainly no expert. I read this in some article on performance
optimization, but can't remember where I'm sorry. However, I have played
around with it and found that it does appear to be true with VC6.
Vince Morgan
- Posted by paresh_chitte2k@rediffmail.com on January 12th, 2005
Vince Morgan wrote:
- Posted by paresh_chitte2k@rediffmail.com on January 12th, 2005
Inline function will avoid your context switching between functions
while function call.
- Posted by paresh_chitte2k@rediffmail.com on January 12th, 2005
Vince Morgan wrote:
- Posted by paresh_chitte2k@rediffmail.com on January 12th, 2005
Vince Morgan wrote:
- Posted by MSCHAEF.COM on January 18th, 2005
In article <1105525142.614069.255900@z14g2000cwz.googlegroups .com>,
<paresh_chitte2k@rediffmail.com> wrote:
For short functions, avoid function call overhead can be a big win. For
debug builds, even more so, since the prologue and epilogue are generally
more complicated (some of the MSVC run-time checking stuff depends on such
code).
-Mike
--
http://www.mschaef.com