R
Rui Maciel
Is there a performance penalty associated with the use of function pointers? If so, how bad is it?
Thanks in advance,
Rui Maciel
Thanks in advance,
Rui Maciel
Is there a performance penalty associated with the use of
function pointers? If so, how bad is it?
Thanks in advance,
Rui Maciel
Rui said:Is there a performance penalty associated with the use of function pointers?
If so, how bad is it?
Is there a performance penalty associated with the use of function pointers? If so, how bad is it?
Thanks in advance,
Rui Maciel
If the code is portable, or even designed to run on different versions of
the same operating system, or on different but binary-compatible hardware,
then that's not a very viable strategy. It will tell you something, but the
figures could change when the Pentium is replaced by the Hexium,.
You could suggest that it is, in fact, faster for an oft called
function. No 32/64 bit address read required each and every call.
Rui Maciel said:Is there a performance penalty associated with the use of function pointers? If so, how bad is it?
Nope, function pointers are faster.
Phil
Phil Carmody a écrit :
What? AN indirect call using a function pointer is faster than a direct
call?
You are just talking nonsense (as always).
What? AN indirect call using a function pointer is faster than a direct
call?
You are just talking nonsense (as always).
All Carmodey brings to this group is a sycophantic blind support for
Heathfield, and a lot of bile directed against anyone and everyone who
dares to point out how inadequate his purported knowledge of C is.
A human has got some insight into how a processor works.He can predict the
future to some extent, if an expert. (I'm not, I didn't realise the cache
effects could be so severe as Jacob describes).
Nick said:what? The function address will be sitting in a register?
I'd not thought of that.
Why? What would force you to do that?
Rui said:Is there a performance penalty associated with the use of function
pointers? If so, how bad is it?
Its no more indirect than anything else if the compiler does its job in
the case of an oft called address stored in a register.
Malcolm McLean said:With an aggressively optimising compiler there shouldn't be much of a
penalty, in typical use.
This is because you use a function pointer to make a function more general.
The classic example form the library is qsort(). Typically this will be used
as
int compfunc(const void *e1, const void *e2)
{
const char **str1 = e1;
const char **str2 = e2;
return strcmp(*str1, *str2);
}
...
qsort(array, N, sizeof(char *), compfunc);
...
So if there is significant penalty in calling a function indirectly, in
machine code, a compiler can replace the indirect jump with a hard-linked
one, or even inline the function totally.
Stephen said:It completely depends on the system you're using; on some it may be
significant, while on others it may be negligible. Measure for yourself
and find out.
Embedded CPUs may not have a branch target predictor at all because it
adds die size, heat, power consumption, and cost. In that case, an
indirect branch is going to hurt every time, regardless of whether the
pointer is changed, because the CPU isn't even _attempting_ to predict
what the target is and has to stall the pipeline to find out.
</OT>
jacob navia said:Gordon Burditt a écrit :
The 64 bit AMD-Intel processor is not a fully 64 bit processor.
The CALL instruction accepts a signed 2GB offset. If you want the
code that you generate to be able to be relocated in memory
ANYWHERE within the 64 bit address space you can't use plain
call instructions and you have to go through an indirect
call.
jacob said:Gordon Burditt a écrit :
The 64 bit AMD-Intel processor is not a fully 64 bit processor.
The CALL instruction accepts a signed 2GB offset. If you want the
code that you generate to be able to be relocated in memory
ANYWHERE within the 64 bit address space you can't use plain
call instructions and you have to go through an indirect
call.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.