GCC optimization for CPU and MEMORY usage

时间:2015-06-15 15:16:18

标签: gcc memory cpu-usage compiler-optimization

Is there a way to optimize the GCC compiled code in term of cpu and memory using option flags? Using O3 rather than 01 does increase or decrease the amount of memory or cpu usage?

3 个答案:

答案 0 :(得分:3)

关于内存使用情况:

  • class MyForm(forms.ModelForm): def __init__(self, *args, **kwargs): self.user = kwargs.pop('user') # To get request.user. Do not use kwargs.pop('user', None) due to potential security hole super(MyForm, self).__init__(*args, **kwargs) # If the user does not belong to a certain group, remove the field if not self.user.groups.filter(name__iexact='mygroup').exists(): del self.fields['confidential'] 减少了程序的二进制大小。它对运行时内存使用有很强的限制影响(C / C ++内存分配和释放是“手动”)。

    我说有限,因为https://www.newlink.org/doc/?doc=可以降低堆栈使用率(此优化也将使用-Os / -O2执行。)

  • -O3tail recursion optimization)选项也可以降低二进制文件大小。

CPU使用率:

可能你应该使用-O3并且不用担心:如果你想节省电量/内存,你的应用程序的整体设计将比编译器开关更有效。

答案 1 :(得分:1)

您可以尝试使用-Os -O2(良好的CPU速度),同时尝试减少二进制文件大小。

查看各种优化here

答案 2 :(得分:0)

上面解决了代码大小优化问题。

我只关注CPU优化。您可以编写具有低处理器利用率的非常好/优化的代码,以及最大化CPU利用率的非常糟糕/未优化的代码。

那么你如何最有效地使用你的处理器?

  1. 首先,使用一个好的优化编译器。我不会和GCC说话,但英特尔和其他一些购买的编译器(例如PGI)非常擅长优化。
  2. 利用底层硬件,例如矢量指令,FMA,寄存器等
  3. 遵循使用外围设备的最佳做法,例如手机,wifi,gps等。
  4. 遵循SW设计的最佳实践,例如延迟隐藏,避免使用中断进行轮询,如果适用,请使用线程池等。
  5. 祝你好运。