Author: Dann Corbit
Date: 12:32:59 05/08/01
Go up one level in this thread
On May 08, 2001 at 00:36:09, Pham Minh Tri wrote:
>Hi,
>
>I write my chess program in C++ (compile by VC6.0). To speed up, I have managed
>to use as many inlines (both implicit and explicit) as posible. Many pieces of
>code have the following form:
>
>class abc {
> void A(int x) { do something. There are 100 places use x; }
> void B() { A(exp); }
>};
>
>(When I call function B, it calls function A with parameter of a short
>expression).
>
>Now I am wondering that compiler may embed directly the expression of parameter
>from function B into A, it means instead of calling that expression one time and
>use its results 100 times, now I may waste time by calling it 100 times!!!
>
>Does anyone has experience about this problem? What exactly the compiler (Visual
>C 6.0) do for this case? (Don't tell your guess, plz).
Inlining may be slower, but not for the reason you express.
When a function is inlined, two good things happen:
1. You lose the function call overhead (small fixed cost).
2. You don't have to jump to a new location in the code (which is another sort
of overhead with variable cost).
Bad things happen too.
The code gets larger. If the code balloons so much that you spill out of the
cache, it might run considerably slower. So just cramming everything as inline
is not a great idea.
If you would call a function one hundred times, then the inline version will
execute one hundred times. But code won't get called more often or less often.
Example:
int foo(int bar)
{
return bar+1;
}
int main(void)
{
int i;
int j;
for (i - 0; i < 10; i++)
j += foo(bar);
}
If not inlined, you will see a call foo in the assembly, a branch to that
location, and increment of the parameter, and the return value getting put in a
register (if it isn't there already). On Intel type chips, it will be in EAX.
If inlined, the code will essentially become:
int main(void)
{
int i;
int j;
for (i - 0; i < 10; i++)
j += (i+1);
}
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.