Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
matanl26478yDoesn't even save time on computation, cache abuse since it destroys locality and what else... Horrible code
-
@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.
-
Ha! What prat wrote that code? Obviously he should have used a for loop instead...
-
elazar10308yThere 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
-
Looking at is again, thanks to notifs; it's also wrong for negative numbers there is no default return.
-
I'm thinking about how even if someone intentionally wanted to write that, they'd run a loop to print the code.
-
jamesh3318yI like how there's a staircase of small travesties leading up to the big one. It helps the reader mentally prepare.
-
OrestH7518yI 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) )
-
jamesh3318y@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.. -
elazar10308y@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)
-
I think convert to string and count length is slightly faster than log? I guess it depends on the language
-
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
-
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.
-
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
You, I heard you have some memory to waste
undefined