类成员函数相对于速度的函数 [英] Class member function vs function with respect to speed

查看:136
本文介绍了类成员函数相对于速度的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

虚拟函数和性能C ++


类成员函数比一个简单的函数需要更多的时间?如果使用继承和虚函数会怎么样?



我试图将我的函数收集到一个简单的接口类中(只有成员函数,没有数据成员)我失去时间。 有解决方法吗?



解决方案

过早优化是所有邪恶的根源

h2>

非静态成员函数接受一个附加参数,它是函数被调用的对象(指针或对象的引用)。这是一个开销。如果函数是virtual,那么在多态调用的情况下也存在一个小间接,即,将函数索引添加到虚拟表基本偏移。这两个开销是sooo可忽略,你不应该担心它,除非profiler说这是你的瓶颈。



过早优化是所有邪恶的根源


Possible Duplicate:
Virtual Functions and Performance C++

Is this correct, that class member function takes more time than a simple function? What if inheritance and virtual functions are used?

I have tried to collect my functions into a simple interface class (only member functions, no data members), and it appears that I lose time. Is there a way to fix it?

P.S. I'm checking with gcc and icc compilers and using -O3 option.

解决方案

Premature optimization is the root of all evil

A nonstatic member function takes an additional argument, which is the object (a pointer or reference to it) on which the function is called. This is one overhead. If the function is virtual, then there is also one small indirection in case of a polymorphic call, that is, addition of the function index to the virtual table base offset. Both of these "overheads" are sooo negligable, you shouldn't worry about it unless the profiler says this is your bottleneck. Which it most likely is not.

Premature optimization is the root of all evil

这篇关于类成员函数相对于速度的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆