0 votes
in C Plus Plus by

Following are the Optimization:

  1. O1: Optimizing compilation at O1 includes more time and memory to break down larger functions. The compiler makes an attempt to reduce both code and execution time. At O1 hardly any optimizations produce great results, but O1 is a setback for an attempt for better optimizations.
  2. // C++ program to calculate the Prime 
  3. // Numbers upto 10000000 using Sieve 
  4. // of Eratosthenes with O1 optimization 
  5. // To see the working of controlled 
  6. // optimization "O1" 
  7. #pragma GCC optimize("O1") 
  8. #include <cmath> 
  9. #include <iostream> 
  10. #include <vector> 
  11. #define N 10000005 
  12. using namespace std; 
  13. // Boolean array for Prime Number 
  14. vector<bool> prime(N, true); 
  15. // Seive implemented to find Prime 
  16. // Number 
  17. void sieveOfEratosthenes() 
  18. { 
  19. for (int i = 2; i <= sqrt(N); ++i) { 
  20. if (prime[i]) { 
  21. for (int j = i * i; j <= N; j += i) { 
  22. prime[j] = false; 
  23. } 
  24. } 
  25. } 
  26. } 
  27. // Driver Code 
  28. int main() 
  29. { 
  30. // Intialise clock to calculate 
  31. // time required to execute without 
  32. // optimization 
  33. clock_t start, end; 
  34. // Start clock 
  35. start = clock(); 
  36. // Function call to find Prime Numbers 
  37. sieveOfEratosthenes(); 
  38. // End clock 
  39. end = clock(); 
  40. // Calculate the time difference 
  41. double time_taken 
  42. = double(end - start) 
  43. / double(CLOCKS_PER_SEC); 
  44. // Print the Calculated execution time 
  45. cout << "Execution time: " << time_taken 
  46. << " secs."; 
  47. return 0; 
  48. } 
  49. O2: Optimizing compilation at O2 optimize to a greater extent. As compared to O1, this option increases both compilation time and the performance of the generated code. O2 turns on all optimization flags specified by O1.
...