Is Loop Hoisting still a valid manual optimization for C code?

后端 未结 8 1100
感动是毒
感动是毒 2021-01-01 15:08

Using the latest gcc compiler, do I still have to think about these types of manual loop optimizations, or will the compiler take care of them for me well enough?

8条回答
  •  离开以前
    2021-01-01 15:34

    Where they are likely to be important to performance, you still have to think about them.

    Loop hoisting is most beneficial when the value being hoisted takes a lot of work to calculate. If it takes a lot of work to calculate, it's probably a call out of line. If it's a call out of line, the latest version of gcc is much less likely than you are to figure out that it will return the same value every time.

    Sometimes people tell you to profile first. They don't really mean it, they just think that if you're smart enough to figure out when it's worth worrying about performance, then you're smart enough to ignore their rule of thumb. Obviously, the following code might as well be "prematurely optimized", whether you have profiled or not:

    #include 
    
    bool isPrime(int p) {
        for (int i = 2; i*i <= p; ++i) {
            if ((p % i) == 0) return false;
        }
        return true;
    }
    
    int countPrimesLessThan(int max) {
        int count = 0;
        for (int i = 2; i < max; ++i) {
            if (isPrime(i)) ++count;
        }
        return count;
    }
    
    int main() {
        for (int i = 0; i < 10; ++i) {
            std::cout << "The number of primes less than 1 million is: ";
            std::cout << countPrimesLessThan(1000*1000);
            std::cout << std::endl;
        }
    }
    

    It takes a "special" approach to software development not to manually hoist that call to countPrimesLessThan out of the loop, whether you've profiled or not.

提交回复
热议问题