Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!think.com!mintaka!bloom-picayune.mit.edu!athena.mit.edu!jfc From: jfc@athena.mit.edu (John F Carr) Newsgroups: comp.lang.c Subject: Re: function pointer overhead? Message-ID: <1990Dec11.102147.19715@athena.mit.edu> Date: 11 Dec 90 10:21:47 GMT References: Sender: news@athena.mit.edu (News system) Organization: Massachusetts Institute of Technology Lines: 26 In article bglenden@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@athena.mit.edu)