first sorry for my poor English.
I want to know Which one of them is the faster
set_name.lower_bound(key) ;
or
lower_bound(set_name.begin() , set_name.end() , value)
I try to know which one of them is the faster.
CodePudding user response:
If in doubt you should perform an analysis of the runtime. However, assuming your set_name variable has type std::set<T> for some T, std::lower_bound is most likely going to lose against std::set<T>::lower_bound.
Why? The latter can use the underlying structure of the rb-tree while the former cannot. In particular, std::lower_bound uses something akin to std::advance. For data structures like an std::vector advance is a constant-time operation (more specifically: a pointer-integer addition). However, for something like an std::set<T>, advance is linear in time, with some hefty pointer indirection and likely repeated cache misses, making it extremely slow to run repeatedly on large containers.
So, if you have a set-like structure, use the lower_bound member structure as it is specifically optimised for that data structure.
