I wanted to know how fast is a single-inheritance virtual function call when compared to one same boost::function call. Are they almost the same in performance or is boost::function slower?
I’m aware that performance may vary from case to case, but, as a general rule, which is faster, and to a how large degree is that so?
Thanks,
Guilherme
— edit
KennyTM’s test was sufficiently convincing for me. boost::function doesn’t seem to be that much slower than a vcall for my own purposes. Thanks.
As a very special case, consider calling an empty function 109 times.
Code A:
Code B: (with boost 1.41):
Compile with
g++ -O3, then time withtime,Inspecting the assembly code, it seems that the slowness may be due to exceptions and handling the possibility and that
fcan be NULL. But given the price of oneboost::functioncall is only 2.4 nanoseconds (on my 2 GHz machine), the actual code in yourdo_x()could shadow this pretty much. I would say, it’s not a reason to avoidboost::function.