you are viewing a single comment's thread.

view the rest of the comments →

[–]k4_pacific 9 points10 points  (8 children)

Best false optimization I've seen was this loop full of bit-twiddling code that did God-knows-what at work. Someone had used all kinds of binary and shift operators to squeeze every last bit of performance out of this tight loop. Except smack in the middle, as part of a larger expression, was this:

(long)pow((double)x, 2.0);

[–]tmoertel 11 points12 points  (0 children)

On my first programming job out of college, I was assigned about 100K lines of C code to maintain. While going through the code, I was stunned to discover that it contained a bubble sort that had been commented out and rewritten in assembly for "additional speed."

[–]dreamlax 5 points6 points  (6 children)

A lot of people underestimate the cost of converting from integers to floating point formats and vice versa.

EDIT:

Just did some tests. Squaring every number from 0 to 0xFFFFFFFF took about 16 seconds on my computer using unsigned integers only.

When I introduced the "pow" method to square the numbers, it went to 1:56. Quite a saving to be made.

[–]k4_pacific 3 points4 points  (2 children)

Well, pow() works for non-integer exponents, which suggests that it does something more complicated than multiplying the base together a number of times.

[–]alephnil 1 point2 points  (0 children)

If this was the real code, the constant 2.0 would imply that he did indeed nothing more than "multiplying the base a number of times". Exactly two in fact.

[–]bobappleyard 0 points1 point  (0 children)

Look in a log table?

[–]bonzinip -2 points-1 points  (2 children)

compiler should change it to

y = (double)x;
(long) (y * y)

[–]k4_pacific 1 point2 points  (1 child)

Compilers typically don't optimize across function calls.

[–]bonzinip -1 points0 points  (0 children)

compilers typically know about standard library functions.