Friday, May 22, 2009

(Function Objects)Functor are faster than Functions

In my play with my pet project, I observed that functors are slightly faster that functions -- I had thought the other way around.

Here the explanation I found online:

"... consider using function objects instead of functions is faster code. The difference between a function and an object of a class that defines the member operator()() is that functions are converted to pointers when passed as arguments to other functions, thus incurring the overhead of a function call even if they are declared inline. In many cases an invocation of a function object, as in the examples on std::transform() in Section 3.2.2.1, can be expanded in-line, eliminating the overhead of a function call."

No wonder.

No comments:

Post a Comment