Hilarious [Or Not So Hilarious] Code Optimizations

2020-05-30 01:07发布

Raymond Chen has this to say on his recent post on code optimizations... Obvious optimizations - one that begs to be optimized - tend to be "de-optimizations" if you consider all that need to be considered...

I'm sure you must have come across / even coded optimizations you were embarrassed about after you learnt more...

Care to share?

4条回答
贪生不怕死
2楼-- · 2020-05-30 01:42

My favourite is

// original code
int a[10];
a[5] = 3;

// optimized code
int a[10];
*(a + 5) = 3;

Yes, all of a sudden, that's magically faster!!</sarcasm>

查看更多
够拽才男人
3楼-- · 2020-05-30 01:48

My favorite example would be the XOR swap algorithm:

// swap these two values:
int x = 4;
int y = 2;
// original:
int temp = x;
x = y;
y = temp;
// optimized version:
x ^= y;
y ^= x;
x ^= y;

Yes, it uses no temporary variable, and can usually be done in three processor cycles, but it sure isn't obvious what it does!

查看更多
▲ chillily
4楼-- · 2020-05-30 01:50
戒情不戒烟
5楼-- · 2020-05-30 01:56

Duff's Device, which is so twisted that it looks like it shouldn't even compile in ISO C:

int n = (count + 7) / 8;
switch (count % 8) {
case 0: do { *to = *from++;
case 7:      *to = *from++;
case 6:      *to = *from++;
case 5:      *to = *from++;
case 4:      *to = *from++;
case 3:      *to = *from++;
case 2:      *to = *from++;
case 1:      *to = *from++;
           } while (--n > 0);
}
查看更多
登录 后发表回答