问题描述
类成员函数比一个简单的函数需要更多的时间?如果使用继承和虚函数会怎么样?
我试图将我的函数收集到一个简单的接口类中(只有成员函数,没有数据成员)我失去时间。 有解决方法吗?
过早优化是所有邪恶的根源 h2>
非静态成员函数接受一个附加参数,它是函数被调用的对象(指针或对象的引用)。这是一个开销。如果函数是virtual,那么在多态调用的情况下也存在一个小间接,即,将函数索引添加到虚拟表基本偏移。这两个开销是sooo可忽略,你不应该担心它,除非profiler说这是你的瓶颈。
过早优化是所有邪恶的根源
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
这篇关于类成员函数相对于速度的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!