GNU OPTIMIZATIONS
A large variety of optimizations are provided by GCC.These reduce the size of the machine code or some of them try to make it run faster at the expense of size and yet others may try to improve its ability to debug the code.Depending on them there are different levels of optimizations :
LEVEL 1 : -O1
Command: gcc -O1 -o test test.c
The goal is to reduce size of compiled program while increasing its performance.You can check the time using commnad time ./a.out
LEVEL 2 : -O2
Command : gcc -O2 -o test test.c
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. The code that takes 10-11 seconds to execute will take less than half the time.
LEVEL 2.5: -Os
Command : gcc -Os -o test test.c
The special optimization level (-Os or size) enables all -O2 optimizations that do not increase code size; it puts the emphasis on size over speed. This includes all second-level optimizations, except for the alignment optimizations
LEVEL 3: -O3
Command: gcc -O3 -o test test.c
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 and -ftree-vectorize options.
Besides these options you can even give architecture specifications that can further improve the results.
Command :gcc -lstdc++ -march=i686 -O3 test.cpp -o test.out [Ref: http://www.linuxjournal.com/article/7269]
Also techies can check http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
for further information on the subject.
You can check astounding differences in time taken by compiler, by executing this code with different optimizing options.
#include
#include
int main() {
//std::cout << omp_get_max_threads() << std::endl;
const int N = 1 << 10;
const int M = 1 << 14;
int a[N];
int i=0;
int j=0;
// #pragma omp parallel for
for (i = 0; i < N; ++i)
a[i] = 0;
// #pragma omp parallel for
for (i = 0; i < N; ++i)
for (j = 0; j < M; ++j)
a[i] += j % N + 3;
printf("%d", a[7]);
printf("\n");
return 0;
}
1:57 AM
|
Labels:
GNU,
optimization
|
This entry was posted on 1:57 AM
and is filed under
GNU
,
optimization
.
You can follow any responses to this entry through
the RSS 2.0 feed.
You can leave a response,
or trackback from your own site.
0 comments:
Post a Comment