搞笑(或不太搞笑)代码优化 [英] Hilarious [Or Not So Hilarious] Code Optimizations

查看:88
本文介绍了搞笑(或不太搞笑)代码优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Raymond Chen在最近的帖子关于代码优化...如果您考虑了所有需要考虑的因素,那么显而易见的优化-乞求进行优化-往往是去优化".

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...

想分享吗?

推荐答案

达夫的设备,它是如此扭曲,以至于它甚至都不应该在ISO C中进行编译:

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);
}

这篇关于搞笑(或不太搞笑)代码优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆