I heard on a forum using std::function<>
causes performance drop. Is it true? If true, is it a big performance drop?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
This depends strongly if you are passing the function without binding any argument (does not allocate heap space) or not.
Also depends on other factors, but this is the main one.
It is true that you need something to compare against, you can't just simply say that it 'reduces overhead' compared to not using it at all, you need to compare it to using an alternative way to passing a function. And if you can just dispense of using it at all then it was not needed from the beginning
Firstly, the overhead gets smaller with the inside of the function; the higher the workload, the smaller the overhead.
Secondly: g++ 4.5 does not show any difference compared to virtual functions:
main.cc
impl.cc
Output of
g++ --std=c++0x -O3 impl.cc main.cc && ./a.out
:So, fear not. If your design/maintainability can improve from prefering
std::function
over virtual calls, try them. Personally, I really like the idea of not forcing interfaces and inheritance on clients of my classes.You can find information from the boost's reference materials: How much overhead does a call through boost::function incur? and Performance
This doesn't determine "yes or no" to boost function. The performance drop may be well acceptable given program's requirements. More often than not, parts of a program are not performance-critical. And even then it may be acceptable. This is only something you can determine.
As to the standard library version, the standard only defines an interface. It is entirely up to individual implementations to make it work. I suppose a similar implementation to boost's function would be used.
There are, indeed, performance issues with
std:function
that must be taken into account whenever using it. The main strength ofstd::function
, namely, its type-erasure mechanism, does not come for free, and we might (but not necessarily must) pay a price for that.std::function
is a template class that wraps callable types. However, it is not parametrized on the callable type itself but only on its return and argument types. The callable type is known only at construction time and, therefore,std::function
cannot have a pre-declared member of this type to hold a copy of the object given to its constructor.Roughly speaking (actually, things are more complicated than that)
std::function
can hold only a pointer to the object passed to its constructor, and this raises a lifetime issue. If the pointer points to an object whose lifetime is smaller than that of thestd::function
object, then the inner pointer will become dangling. To prevent this problemstd::function
might make a copy of the object on the heap through a call tooperator new
(or a custom allocator). The dynamic memory allocation is what people refer the most as a performance penalty implied bystd::function
.I have recently written an article with more details and that explains how (and where) one can avoid paying the price of a memory allocation.
http://drdobbs.com/cpp/232500059