【问题标题】:Is there are flag in gcc compiler for pthread code to minimize execution time?gcc 编译器中是否有用于 pthread 代码的标志以最小化执行时间?
【发布时间】:2013-09-18 10:46:00
【问题描述】:

我正在用 C 语言编写 pthread 代码,并使用 gcc 编译器。我已经用 pthread_condition、互斥锁和信号量实现了一个代码。gcc 中是否有任何标志或选项来提高执行时间?

编写程序来解决这个问题Problem

【问题讨论】:

  • 谁能回复我??为什么要对这个问题投反对票?

标签: c multithreading gcc pthreads


【解决方案1】:

gcc 联机帮助页显示:

   -O
   -O1 Optimize.
        Optimizing compilation takes somewhat more time, and a lot more
        memory for a large function. With -O, the compiler tries to reduce
        code size and execution time, without performing any optimizations
        that take a great deal of compilation time.
   -O2 Optimize even more.
        GCC performs nearly all supported optimizations that do not involve a
        space-speed tradeoff. As compared to -O, this option increases both
        compilation time and the performance of the generated code.
   -O3 Optimize yet more.
       -O3 turns on all optimizations specified by -O2 and also turns on the
       -finline-functions, -funswitch-loops, -fpredictive-commoning,
       -fgcse-after-reload, -ftree-vectorize and -fipa-cp-clone options.

因此,如果您希望代码运行得更快(“最小化执行时间”),一个好的开始是使用-O3

由于优化将是通用的,因此您必须进行大量基准测试才能获得给定代码的最佳结果。

【讨论】:

  • 谢谢@umläute 是否有针对多线程特定的标志?无论如何我正在使用 -g -D_FILE_OFFSET_BITS=64 -Wall -O2 -finline-functions?正如我猜测的那样,线程必须有任何特殊标志?
  • @Vishwadeep,你为什么不能自己阅读这本精彩的手册?在 x86 上,您必须使用 -pthread 来编译和链接多线程程序,但没有单独的优化选项。慢速多线程应用程序通常是由于选择不当的同步和缓存效果(例如错误共享)而不是由于编译器优化
  • 感谢@JonathanWakely 我阅读了它并且 -lpthread 和休息标志(我在之前的评论中提到过)已经在使用它们.. 但我问了一个问题,因为有人知道超出我理解限制或愿意的任何额外选项分享知识..
  • 正是……像互斥锁和 pthread 条件是要优化的一大块代码……因此我开始思考使用优化标志的这种方式……
  • @Vishwadeep:请注意-lpthread-pthread 之间的区别。延伸阅读:stackoverflow.com/q/2127797/694576
猜你喜欢
  • 1970-01-01
  • 2019-11-18
  • 2015-01-27
  • 1970-01-01
  • 2013-02-25
  • 1970-01-01
  • 2022-01-19
  • 2011-01-08
  • 1970-01-01
相关资源
最近更新 更多