3 ms·
In C++, is empty() faster than comparing the size with zero?
- jamesfinlayson 5y agoThat's quite a handy optimisation from gcc - I only dabble in C++ so maybe it would be obvious to someone that uses it day to day, but I had a good look at that code and couldn't see any obvious way to make is_empty O(1) based on its implementation.
- Miiko 5y agoTwo questions: who is Travis Downs, and why he decided to re-implement 'size' method with different name 'count_nodes' and O(N) complexity?
- htk 5y agoWhy is is_empty comparing to bigger than zero instead of equals?
- jb1991 5y agoThat’s a common optimization, not just with this particular requirement. Choose the thing that is most ambiguous that gives you the answer you need, and it’s more likely to result in a simpler hardware instruction.
- rafaelvasco 5y agoProbably because , for this case, comparing for > or < is faster than equality comparison. Which from my experience is true.
- htk 5y agoThe author edited the article and corrected is_empty to is_not_empty.