151

You, I heard you have some memory to waste

Comments
  • 14
    Doesn't even save time on computation, cache abuse since it destroys locality and what else... Horrible code
  • 29
    What? Why would this shit return a long long?
  • 13
    Are you fucking kidding me... This code is a disgrace...
  • 8
    @filthyranter because they are dumb. They thought that if you give a long long, you should get a long long, even though it maxes out in value at fucking 18.
  • 13
    looks like ascii art
  • 6
    * code porn!
  • 9
    Technically correct... B+
  • 6
    Return num.length()-1;

    Except for first use case roughly, treat num as string
  • 12
    @hoggchan it's more like code rape and homicide.
  • 24
    I use a one liner for that; see pseudocode:
    int n
    nLength = floor(log10(n)) + 1
  • 3
    Ha! What prat wrote that code? Obviously he should have used a for loop instead...
  • 1
  • 3
    There may be good reasons for writing such code, although I believe there are none in this case, as proven by the return type and the redundant >= tests
  • 3
    @bkwilliams this is floating point math, you don't need it here
  • 2
    @iam13islucky I know that the author of that code is dumb.
  • 16
    Come on guys, at least it's got documentation 👍😎👌
  • 2
    Looking at is again, thanks to notifs; it's also wrong for negative numbers there is no default return.
  • 1
    @bkwilliams +1 for thinking log10 :)
  • 4
    This hurts me, i am cry
  • 3
    I'm thinking about how even if someone intentionally wanted to write that, they'd run a loop to print the code.
  • 2
    I like how there's a staircase of small travesties leading up to the big one. It helps the reader mentally prepare.
  • 1
    I predict, that you haven't ever heared about logarithms =) a guy above posted a good inline form of counting nunber of digits in the number ( floor(log10(x)) + 1) )
  • 2
    @OrestH my guess actually is the code in the image is more efficient (discounting the long long and unnecessary comparisons) because there would be one instruction for each case.

    A log is probably a few instructions, one more for add, one comparison...

    I'll confess I probably would have used sprintf and strlen because I'm lazy and that's the worst option..
  • 1
    @alebianco the cpu will likely execute the instructions out of order - i.e. in parallel in the same core, since there are no real interdependencies there. So it may very well be faster than any floating-point operation (or a conversion to string, or an unrolled loop)
  • 0
    I think convert to string and count length is slightly faster than log? I guess it depends on the language
  • 1
    What really bugs me is that on line 10 the semicolon is suddenly displaced by 3 to the right instead of 2 on the other lines
  • 1
    This is being reposted too much
  • 2
    @mzpro10 this was posted 3 weeks ago.
  • 0
    The patience it took to write this....
  • 1
    @soulkicks it was dead, no necromancy!!
  • 0
    @iam13islucky what is dead may never die
  • 0
    You know, if you don't want to use one-liners, you could always use a counter and a temporary clone of the value to calculate the length inside of a while-loop with the condition temp != 0.
  • 0
    return num.toString().length;
  • 1
    Is he paid by the number of lines of code that he writes?
  • 0
    int length = 1;
    int temp = numberToCheck / 10; // Integer division
    while (temp != 0)
    {
    length++;
    temp /= 10; // Integer division
    }

    // Might not be the most efficient way to do this but it's simple and probably more efficient than converting it into a string
    // Derived from getting the digit sum
Add Comment