c++ - Speed of bound lambda (via std::function) vs operator() of functor struct -


auto lam = [](int a, int b, int c) { return < b && b < c; };  struct functor {   int a;   int b;   bool operator()(int n) const { return < n && n < b; } }; 

in version one, we

std::vector<std::function<bool (int)>> lamvals; // parameters , each lamvals.emplace_back(std::bind(lam, a, std::placeholders::_1, b)); 

the alternative is

std::vector<functor> lamvals; // parameters , each lamvals.emplace_back(functor{a, b}); 

in both cases have simple iteration

    return std::any_of(lamvals.cbegin(), lamvals.cend(),             [n](const decltype(lamvals)::value_type & t){return t(n);}); 

i seeing speed difference of 3:1, bound lambda slower. functor fast storing integer pairs , hardcoding tests. obviously, hardcoding not useful production code, because there several functions in play, not between. however, can go either many functors or many lambdas. latter fewer lines of code , looks cleaner, don't think can afford speed difference; code in critical loop.

i looking speedup suggestions.

the difference between 2 cases fundamentally functor, compiler knows called @ compile time, function call can inlined. lambdas, interestingly enough, have unique type. means again, when use lambda, @ compile type (since compiler must know types) function being called known, inlining can occur. on other hand, function pointer type based on signature. signature must known can called , returned appropriately, other function pointer can point @ run-time, far compiler concerned. same true std::function.

when wrap lambda in std::function, erase type of lambda compiler perspective. if sounds weird/impossible, think of way: since std::function of fixed type can wrap callable same signature, compiler has no way of knowing other instruction won't come alone , change std::function wrapping.

this link, http://goo.gl/60qfjh, shows mean (by way, godbolt page very handy, suggest getting acquainted it). wrote 3 examples here similar yours. first uses std::function wrapping lambda, second functor, third naked lambda (unwrapped), using decltype. can @ assembly on right , see both of latter 2 inlined, not first.

my guess can use lambdas same thing. instead of bind, can value based capture lambdas of , b. each time push lambda vector, modify , b appropriately, , voila.

stylistically though, feel should use struct. it's clearer what's going on. mere fact seeming want capture , b in 1 place, , test against c in another, means used in code in not 1 place. in exchange like, 2 lines of code, more readable, easier debug, , more extensible.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -