Re:In C++, is empty() faster than comparing the size with zero?
by not2b on Thursday October 28, 2021 @08:32PM
(#29033876)
Not all O(1) functions have the same cost. Suppose we have a container that stores its data on the heap, and has a null pointer and no heap object when it is empty. Then Container::empty() const just needs to check that pointer, where Container::size() const has to read the size out of the heap object, so c.size() == 0 has to cost more, as the compiler is unlikely to be able to optimize away the extra accesses.
[ Parent
| on HN ]
There are no comments on this comment yet.