Why does GCC generate 15-20% faster code with size optimization (-Os) rather than speed optimization (-O2)?
Hi everyone! I'm new to C++ and trying to understand GCC optimization flags. I have a small image processing program: I apply a Gaussian filter to a pixel matrix (size 4096x4096). I compile it with different flags and measure execution time. As expected, -O2 gives a speedup compared to -O0. But I noticed something strange: with the -Os flag (optimize for code size), the program runs 15-20% faster than with -O2 (optimize for speed)! Here's the simplified code:
```cpp
#include
#include
#include
void gaussian_blur(const std::vector& src, std::vector& dst, int width, int height) {
const float kernel[5] = {0.0545, 0.2442, 0.4026, 0.2442, 0.0545};
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
No answers yet.
Log in to leave an answer