Jun 3, 2020 in C Plus Plus
Q:

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. for (int i = 2; i <= sqrt(N); ++i) { 
  19. if (prime[i]) { 
  20. for (int j = i * i; j <= N; j += i) { 
  21. prime[j] = false; 
  22. // Driver Code 
  23. int main() 
  24. // Intialise clock to calculate 
  25. // time required to execute without 
  26. // optimization 
  27. clock_t start, end; 
  28. // Start clock 
  29. start = clock(); 
  30. // Function call to find Prime Numbers 
  31. sieveOfEratosthenes(); 
  32. // End clock 
  33. end = clock(); 
  34. // Calculate the time difference 
  35. double time_taken 
  36. = double(end - start) 
  37. / double(CLOCKS_PER_SEC); 
  38. // Print the Calculated execution time 
  39. cout << "Execution time: " << time_taken 
  40. << " secs."; 
  41. return 0; 
  42. 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.

Related questions

0 votes
Jan 6 in C Plus Plus
...