Hacker News new | past | comments | ask | show | jobs | submit login

No.

You run into the same problem if you are writing something like the C 'itoa' function (integer to ascii); if you want to write the digits out front to back you need to know what divisor to use for the leading digit so you need to either look it up in a table or take the log.

Taking the log is a lot slower than the table lookup, I found that out the hard way.

People convert so many integers to ascii and it is shocking how slow ascii <-> binary numeric conversions are compared to binary numeric operations, so it's not a matter of "premature optimizations".

Now you can write an itoa which generates the digits from back to front and not have to worry about copying the results because you return a pointer to the middle of the result buffer but then memory management gets more complex...




You can use a binary search to make the lookup even faster. There's no need to iterate over all possible lengths.


I'm not sure that you come out ahead with binary search over n=10 given branch prediction issues. You'd have to test it to really know.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: