function pointer overhead?

John F Carr jfc at athena.mit.edu
Tue Dec 11 21:21:47 AEST 1990


In article <BGLENDEN.90Dec10224133 at mandrill.cv.nrao.edu>
	bglenden at mandrill.cv.nrao.edu (Brian Glendenning) writes:

>Someone once told me that calling through a function pointer can cause
>significant overhead. Is this true? If so, for what types of machines
>is it true?

Most processors' call instructions are optimized to work well with constant
addresses.  On the VAX 3 series, I found the call instruction to be slightly
faster with a constant address than with the address in a register.  The
call instructions on the 2 IBM RISC machines I use (RT and S/6000) take
equal times for calls to constant or variable addresses, but the calling
convention forces an extra memory reference for calling through a pointer
(on the RT, a function pointer is really a pointer to a word which contains
the address of the first instruction in the function; if the function
address is constant the lookup of the instruction address can be optimized
out).  Using a function pointer will probably decrease code size.

I don't know of any architecture where the difference between calling a
constant function address and calling via a function pointer is noticable in
normal use (on the IBM RT, the difference is at most 5 CPU cycles = 500 ns).
If you really care, you should profile your program on the machine you will
be using.

--
    John Carr (jfc at athena.mit.edu)



More information about the Comp.lang.c mailing list