8 ms·
I don't understand. There are 7 suffixes, can't you pick the right one with binary search? That would be 3 comparisons. Or just do it the dumb way and have 6 co
by throwaway9870 3y ago
I don't understand. There are 7 suffixes, can't you pick the right one with binary search? That would be 3 comparisons. Or just do it the dumb way and have 6 comparisons. How are two log() calls, one pow() call and ceil() better than just doing it the dumb way? The bug being described is a perfect example of trying to be too clever.
- emerongi 3y agoThe author apparently went back to using a loop after recognizing that it's not readable: https://programming.guide/java/formatting-byte-size-to-human-readable-format.html https://programming.guide/java/formatting-byte-size-to-human... Notably, it's still slightly better than the first code example in the original article, as it takes the rounding bug into account.
- zeroonetwothree 3y agoThe author says at the beginning that it’s not actually better than the loop. Also 6 comparisons is only if you’d have the max value which seems unlikely in actual usage. Linear could be better if most of the time values are in B or KB ranges