Skip to main content
gamedev.net
🔒 Locked

Speed for Function Pointers and Delegates

Started by wanzi 1 reply 1k views
Original Post
wanzi
wanzi
Hi every one. Recently i start (re)designing my (crappy) physics engine, and wanna add polymorphism(virtual functions) in to make it more flexible. Since virtual functions will probablely introduce some overhead, in some speed critical parts, i'm gonna replace them with function pointers, which i think have the same underlying implementation as normal function call, and should be the fastest i can get. But after reading from somewhere, that isn't always the case.[oh] Another problem is that FP has very bad support and ugly syntax for member functions, which motivates me to find an alternative. After googling, I came across this article, it claims "invoking a single-target delegate is as fast as a normal function call", and has much better syntax ans support for member functions. It really sounds promising. Basically, what i wanna ask are: 1)Is Function pointer call always as fast as normal function call? 2)Will replacing branching(if...elseif...) with FP array be faster? 3) Are Delegates really that fast? 4)Can i widely adopt Deleagtes, and replace all the FP(if possible)? [Edited by - wanzi on October 15, 2005 9:01:02 PM]
Glak
Glak
Virtual functions are faster than any attempt to replace them. Do not try to optimize your code unless you have actual performance numbers or big O complexity differences. If you want an virtual function simply use the virtual keyword in front of your function. It will be faster than any alternative.
Conner McCloud
Conner McCloud
Quote:
Original post by wanzi
Since virtual functions will probablely introduce some overhead, in some speed critical parts, i'm gonna replace them with function pointers...

Not so much, no. See here.

CM

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.