TITLE: the cost of virtual functions (Newsgroups: comp.lang.c++.moderated, 25 Feb 98) KADAR: kadar.tanuwidjaja@dgs.monash.edu.au says... >> I need to speed up my simulation program. As I use quite a lot of >> virtual functions, I think they might introduce quite significant >> overhead. Before I embark in the tedious modification. can someone >> kindly share his/her experience with virtual function overhead. COFFIN: jcoffin@taeus.com (Jerry Coffin) >My experience is that you're far better off using a profiler than >depending on answers you get to a question like this. Using a lot of >virtual functions doesn't really mean much: the question is almost >entirely one of how OFTEN you use them, under what circumstances, etc. >In addition, the actual overhead of using virtual functions can vary >rather widely depending upon the compiler you use. Worse yet, the >alternatives (such as switch statements) vary extremely widely with >the compiler you use as well. > >Since you haven't told us what compiler or architecture you're >developing for/with, it's virtually impossible for anybody to predict >what would/will happen if you convert your virtual functions to some >other code to accomplish the same thing. Worse yet, since we have no >real idea of how much of your run-time is involved in making virtual >function calls, it's absolutely impossible for anybody to guess at >what the impact of such changes might be. CLAMAGE: clamage@Eng.Sun.COM (Steve Clamage) I agree with everything Jerry has said. Let me add some observations. Depending on the platform, the particular C++ implementation, and the particular class hierarchy, the overhead of a particular virtual call compared to calling the same function directly is often 2 to 5 instructions. The virtual call involves a table lookup plus a branch, and might need to add an offset to "this". Sometimes the compiler can determine that the virtual call mechanism does not need to be used for what looks like a virtual call, and the overhead is zero. If the function calling sequence plus function body totals more than 100 instructions, it is unlikely that the virtual-call overhead matters, unless the function is called many times in a tight inner loop. The case that makes the most difference is when a function call cannot be inlined because it is invoked via the virtual call mechanism. As with all questions regarding performance, first use an appropriate design and good coding practices. Then measure performance. If performance is not adequate, look first for more efficient algorithms. Only then look to tweaking code, and only after profiling to determine where the time is actually being spent. And bear in mind that your code tweaks can be self-defeating when you move to a different platform, or even to a different release of the same compiler.