Does the compiler perform the same action for all built-in function calls?

Does the C ++ compiler use different solutions for nesting two different calls for the same inline function?

consider this code:

inline func(int x) {
    return x + (x << 1) + (x << 2) + (x << 3) + (x << 4);
}

int main() {
    int y = func(1) + func(2) + func(3) + func(4);
    for(int i = 0; i < 100000000; ++i)
        y += func(i % 10);
    cout << y << endl;
    return 0;
}

will the compiler perform the same action with calls before the loop and inside the loop? if we consider the length of the code along with speed optimization, then the calls before the loop should not be built in, but inside should be.

+4
source share
1 answer

It depends on your compiler. Let's say you use gcc 5.4.0with an optimization level -O2. The first line inside the functionmain

int y = func(1) + func(2) + func(3) + func(4);

- , for . , .

, "" , - .

+4

Source: https://habr.com/ru/post/1678820/


All Articles