C++虚函数调用与 boost::function 快速调用
我想知道与同一个 boost::function 调用相比,单继承虚函数调用有多快。它们的性能几乎相同还是 boost::function 更慢?
我知道性能可能因情况而异,但是,作为一般规则,哪个更快,速度有多大?
谢谢, Guilherme
-- 编辑
KennyTM 的测试对我来说足够有说服力。出于我自己的目的, boost::function 似乎并不比 vcall 慢多少。谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为一种非常特殊的情况,请考虑调用空函数 109 次。
代码A:
代码B:(使用boost 1.41):
用
g++ -O3
编译,然后用time
计时,检查汇编代码,似乎缓慢可能是由于异常和处理可能性造成的,并且
f
可以为 NULL。但考虑到一次boost::function
调用的时间仅为 2.4 纳秒(在我的 2 GHz 机器上),do_x()
中的实际代码可能会掩盖这一点。我想说,这不是避免使用boost::function
的理由。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
f
can be NULL. But given the price of oneboost::function
call 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
.