你用什么编码技术用于优化C程序? [英] What coding techniques do you use for optimising C programs?

查看:110
本文介绍了你用什么编码技术用于优化C程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几年前,我是在面试应聘者一个相对高级的嵌入式C程序员位置的面板上。

Some years ago I was on a panel that was interviewing candidates for a relatively senior embedded C programmer position.

相反,我所问的是关于优化技术的标准问题。我很惊讶,一些考生没有答案。

One of the standard questions that I asked was about optimisation techniques. I was quite surprised that some of the candidates didn't have answers.

因此​​,在组建为后人名单的利益 - 优化C程序时,你通常用什么方法和构造

So, in the interests of putting together a list for posterity - what techniques and constructs do you normally use when optimising C programs?

解答优化速度和规模都接受了。

Answers to optimisation for speed and size both accepted.

推荐答案

首先第一件事情 - 不优化为时尚早。这并不罕见花时间仔细优化code一大块才发现,这是不是说你认为这将成为瓶颈。或者,换一种说法:你让它快之前,使其工作

First things first - don't optimise too early. It's not uncommon to spend time carefully optimising a chunk of code only to find that it wasn't the bottleneck that you thought it was going to be. Or, to put it another way "Before you make it fast, make it work"

调查是否有针对优化code前的算法优化的任何选项。它会更容易通过优化算法不佳找到性能的改善比它优化code,只有当您更改算法无论如何把它扔掉。

Investigate whether there's any option for optimising the algorithm before optimising the code. It'll be easier to find an improvement in performance by optimising a poor algorithm than it is to optimise the code, only then to throw it away when you change the algorithm anyway.

和工作了,为什么你需要摆在首位,以优化。你到底想达到什么目的?如果你想,说,提高响应时间某个事件的工作是否有改变执行的顺序,以尽量减少时间的关键领域提供了机会。例如试图改善一些外部中断,你可以做的事件之间的死区时间任何preparation的反应是什么时候?

And work out why you need to optimise in the first place. What are you trying to achieve? If you're trying, say, to improve the response time to some event work out if there is an opportunity to change the order of execution to minimise the time critical areas. For example when trying to improve the response to some external interrupt can you do any preparation in the dead time between events?

一旦你决定,你需要优化code,你该优化位?使用分析器。上使用频率最高的地区集中注意力(第一个)。

Once you've decided that you need to optimise the code, which bit do you optimise? Use a profiler. Focus your attention (first) on the areas that are used most often.

那么,你可以做一下那些方面?

So what can you do about those areas?


  • 最小化条件检查。检查条件(例如。对于循环终止条件)是没有被实际处理花费的时间。条件检查可以像循环展开技术来最小化。

  • 在某些情况下,条件检查也可以通过使用函数指针消除。例如,如果您正在实现一个状态机,你可能会发现,实施各个国家的小功能处理器(有统一的原型)和存储下一个处理程序的函数指针存储下一个状态比使用更高效大switch语句,在个别情况下,语句实现处理器code。情况因人而异。

  • 减少函数调用。函数通常携带来电保存上下文的负担(如编写包含在寄存器堆栈中的局部变量,保存堆栈指针),所以如果你没有拨打电话,这是节省时间。一种选择(如果你优化的速度,而不是空间)是利用内联函数。

  • 如果函数调用是不可避免的减少被传递给函数的数据。例如传递指针是可能比传递结构更有效。

  • 当优化速度选择那些所用平台的原生大小的数据类型。例如在32位处理器它很可能是更有效的操作的32位值大于8或16位值。 (附注 - 值得一检查,编译器是做什么的,你觉得这是我已经在那里我发现我的编译器坚持做16位运算的8位值与所有的往返转换的情况。和他们一起去)

  • 找到,可以pcalculated $ P $,无论是和在编译时初始化或(更好的)中计算的数据。例如,实现一个CRC您可以实时计算的CRC值(直接使用多项式),这是伟大的大小(但可怕的性能)时,也可以生成所有的中间值的表 - 这是一个更快的实现,以大小的损害。

  • LOCALISE您的数据。如果你正在操作数据一滴常常你的处理器可以通过存储所有在缓存中,加快速度。和你的编译器可能能够使用适合于更多的本地化数据(使用8位偏移量,而不是32位的如:指令)指令短

  • 在同样,本地化的功能。出于同样的原因。

  • 制定出的假设,你可以对您所执行的操作,并找到利用它们的方法。例如,一个8位平台上,如果,在你在一个32位的值做唯一的操作就是你会发现你可以通过内联(或创建宏)专门为此做比编译器更好的增量,而不是使用普通的算术运算。

  • 避免昂贵的指示 - 分裂就是最好的例子

  • 注册关键字可以是你的朋友(虽然希望你的编译器有关于你的寄存器使用一个pretty好主意)。如果你要使用注册很可能是你必须声明你想局部变量注册编第一。

  • 与您的数据类型一致。如果您在数据类型(如短裤和整型,双打和彩车),那么编译器将每个不匹配的隐式类型转换的混合物做算术。这是浪费CPU周期,可能没有必要。

大多数上面列出的选项可以用作普通业务的一部分,没有任何不良影响。但是,如果你真的想竭力维持最佳的性能:
    - 调查,您可以在(安全)禁用错误检查。它不推荐,但它会为您节省一些空间和周期。
    - 在你的汇编code的手工工艺部分​​。当然,这意味着你的code是无法再移动,但在那里,这不是你可以在这里找到节约的问题。要知道,虽然有可能失去的时间数据移入和移出,您在您的处置寄存器(即以满足你的编译器的寄存器使用)。另外要注意,你的编译器应该做的本身就是一个pretty好工作。 (当然也有例外)

Most of the options listed above can be used as part of normal practice without any ill effects. However if you're really trying to eke out the best performance: - Investigate where you can (safely) disable error checking. It's not recommended, but it will save you some space and cycles. - Hand craft portions of your code in assembler. This of course means that your code is no longer portable but where that's not an issue you may find savings here. Be aware though that there is potentially time lost moving data into and out of the registers that you have at your disposal (ie. to satisfy the register usage of your compiler). Also be aware that your compiler should be doing a pretty good job on its own. (of course there are exceptions)

这篇关于你用什么编码技术用于优化C程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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