C编码性能或code尺寸的做法 - 远远超出了一个编译器 [英] C coding practices for performance or code size - beyond what a compiler does

查看:126
本文介绍了C编码性能或code尺寸的做法 - 远远超出了一个编译器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找,看看有什么可以程序员用C做的,可以确定的性能和/或生成的目标文件的大小。

I'm looking to see what can a programmer do in C, that can determine the performance and/or the size of the generated object file.

有关例如,结果
1.简单的声明的get / set函数为内联可以提高性能(在更大的足迹成本)结果
2.对于没有使用循环变量的值本身回路,计数下降到零,而不是向上计数到一定值
等等。

For e.g,
1. Declaring simple get/set functions as inline may increase performance (at the cost of a larger footprint)
2. For loops that do not use the value of the loop variable itself, count down to zero instead of counting up to a certain value etc.

它看起来像编译器现在已经进展到简单的技巧(如上面两个点)不会在所有需要的水平。在编译过程中适当的选项反正做的工作。哎呀,我也看到了帖子这里的编译器手柄如何递归 - 这是非常有趣!那么,什么是我们离开在C水平怎么做呢? :)

It looks like compilers now have advanced to a level where "simple" tricks (like the two points above) are not required at all. Appropriate options during compilation do the job anyway. Heck, I also saw posts here on how compilers handle recursion - that was very interesting! So what are we left to do at a C level then? :)

我的具体环境是:GCC 4.3.3再针对ARM架构(V4)。但在其他编译器的反应/处理器也欢迎和将在被嚼。

My specific environment is: GCC 4.3.3 re-targeted for ARM architecture (v4). But responses on other compilers/processors are also welcome and will be munched upon.

PS:我的这种做法有悖于code首先!,然后基准和的最后的优化的做法

PS: This approach of mine goes against the usual "code first!, then benchmark, and finally optimize" approach.

修改:就像恰巧,我发现了一个类似的帖子张贴问题后:的我们应该仍然可以优化"在小"?

Edit: Just like it so happens, I found a similar post after posting the question: Should we still be optimizing "in the small"?

推荐答案

有一件事我能想到的,编译器可能不会优化是缓存友好性:如果你遍历一个二维数组行优先的顺序,也就是说,要确保在整个列索引你的内循环运行,以避免高速缓存抖动。其运行在错误的索引内循环会导致巨大的性能损失。

One thing I can think of that a compiler probably won't optimize is "cache-friendliness": If you're iterating over a two-dimensional array in row-major order, say, make sure your inner loop runs across the column index to avoid cache thrashing. Having the inner loop run over the wrong index can cause a huge performance hit.

这适用于所有的编程语言,但如果你在C语言进行编程,性能可能是你的关键,所以这是特别重要。

This applies to all programming languages, but if you're programming in C, performance is probably critical to you, so it's especially relevant.

这篇关于C编码性能或code尺寸的做法 - 远远超出了一个编译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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