Efficient way to extract number of digits

integer
logarithm
maths

#1

I was wondering about the most effective (and fast) way to extract number of digits from an integer. I’ve come to two conclusions:

  • Old while() loop (divide until digits left)
  • log10 (logarithm to base 10)

Which one of these is faster? I had this doubt because I made out that the while() loop requires modulo (%) operation, which is very complex, whereas log10 is another complex math operation.

Ref: https://stackoverflow.com/questions/1489830/efficient-way-to-determine-number-of-digits-in-an-integer


#2

for smaller values doing while loop should be an efficient option but as we always say it is up to the size of the data and for some particular case which one will be more efficient is based on the probability that if data is small or large.
for large data, log is a better option but for smaller values, while loop is just awesome
also as the difference between the two is very less it should not drastically affect the time

for the implementation of log i took into consideration while writing this
[http://www.netlib.org/fdlibm/e_log.c]

that link might not be working, i dont know why but here is stackoverflow from where i picked this thing up so if that one doesn’t work try using this
stackoverflow link


#3

Thanks a lot man! :slight_smile: