0

I am writing a pthread code in C, and using gcc compiler. I have implemented a code with pthread_condition, mutex locks and semaphores.. Is there any flag or option in gcc to enhance the execution time?

Program is written to solve this Problem

Community
  • 1
  • 1
Vishwadeep Singh
  • 1,043
  • 1
  • 13
  • 38

1 Answers1

4

the gcc manpage reveals:

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

so if you want your code to run faster ("minimize execution time"), a good start is to use -O3.

since the optimizations will be generic, you will have to do a lot of benchmarking to get best results for a given code.

umläute
  • 28,885
  • 9
  • 68
  • 122
  • Thanks @umläute is there any flag for multi threading specific? Anyhow i am using -g -D_FILE_OFFSET_BITS=64 -Wall -O2 -finline-functions? As i was guessing there must be any special flag for threads? – Vishwadeep Singh Sep 18 '13 at 11:01
  • 1
    @Vishwadeep, why can't you read the fabulous manual yourself? On x86 you must use `-pthread` to compile and link multithreaded programs, but there are no separate optimisation options. Slow multithreaded applications are usually due to poor choices of synchronisation and cache effects (e.g. false sharing) not due to compiler optimisations – Jonathan Wakely Sep 18 '13 at 11:05
  • 1
    Thanks @JonathanWakely I read it and -lpthread and rest flags (which i had mentioned in previous comment) already using them.. but i asked question because someone knows any extra option beyond my understanding limits or willing to share knowledge.. – Vishwadeep Singh Sep 18 '13 at 11:09
  • Exactly.. like mutex locking and pthread condition is a big chunk of the code to be optimized... Hence i started thinking in this way of using optimization flags.. – Vishwadeep Singh Sep 18 '13 at 11:12
  • 1
    @Vishwadeep: Please note the **difference** between `-lpthread` and `-pthread`. Further reading: http://stackoverflow.com/q/2127797/694576 – alk Sep 18 '13 at 12:57