33
mak420
8y

seems legit xD

Comments
  • 0
    Wondering, what's the best way to write this?

    Switch?
    Simpler ifs?
    Counting digits?
    ?
  • 1
    @nickpapoutsis please... just no...
  • 0
    @No-one the way i would do that too
  • 0
    @No-one Na okay. But I think there are multiple ways to count digits
  • 0
    @No-one Yeah. When doing it with ints its also possible by dividing by 10 until value is 0. Dont forget to count times until its 0.
  • 4
    int i =0;
    while(num>0)
    {
    num/=10;
    i++;
    return i;
    }
  • 0
    @No-one Counting digits is a simple one liner but is it the best way (performance wise)?
  • 1
    That's hilarious. Where'd you even find that?
  • 0
    Or... you know... floor(log10(number))
  • 0
    Well...

    https://jsperf.com/170313

    OS: Win10 Pro Insider 15055

    On FF Dev 54.0.0 [12/03/17] (heavily modified profile with extensions and 100+ tabs) case 1 (simple ifs) wins with 800 million ops.
    Cases 3-6 (toString, log10) are around 31-36 million ops.
    Cases 7, 8 (continuous division by 10) 483 million ops best case, 6,5 million ops worst case.
    Cases 9, 10 (else if) are marginally worse than simple ifs.

    On Chrome Dev 58.0.3029 (relatively clean, a few extensions, only tab) I am getting a lot less ops (about 20 times less) than FF, not sure why.
    Cases 1 and 9 (best case ifs, else ifs) are on top with 4,4 million ops.
    Cases 2 and 10 (worst case ifs, else ifs) are at 2,3 million ops.
    Cases 3-6 (toString, log10) are at 3 to 3, 5 million ops.
    Cases 7, 8 (continuous division by 10) 4,3 million ops best case, 1,3 million ops worst case.

    Edge follows same pattern as Chrome but with much higher ops.

    Feedback?

    PS: I'm not a dev, excuse any mistakes.
  • 0
    Can anyone explain the Chrome results?

    I thought their engine was much faster than Firefox's.
  • 0
    @popu88 wrong !
  • 1
    You might be laughing at this, but this is actually the most efficient approach if you're working with very CPU constrained applications (embedded programming).
  • 1
  • 0
    I got it @mekna return i; outside the loop
Add Comment