Finding the number of digits of an integer

前端 未结 17 2195
难免孤独
难免孤独 2020-12-07 16:32

What is the best method to find the number of digits of a positive integer?

I have found this 3 basic methods:

  • conversion to string

             
    
    
            
17条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-07 16:55

    I don't know, and the answer may well be different depending on how your individual language is implemented.

    So, stress test it! Implement all three solutions. Run them on 1 through 1,000,000 (or some other huge set of numbers that's representative of the numbers the solution will be running against) and time how long each of them takes.

    Pit your solutions against one another and let them fight it out. Like intellectual gladiators. Three algorithms enter! One algorithm leaves!

提交回复
热议问题