Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does the compiler optimize this piece of code

consider the following loop:

unsigned long x = 0;
for(unsigned long i = 2314543142; i > 0; i-- )
    x+=i;
std::cout << x << std::endl;

when I compile this normally it take roughly 6.5 seconds to execute this loop. But when I compile with -O3 optimization the loop gets executed in 10^-6 seconds. How is this possible? The compiler surely does not know how the closed form expression for x...

like image 825
user3726947 Avatar asked Dec 19 '25 13:12

user3726947


1 Answers

You don't really have to know all about assembly to see that the complier determines the value of x at compile time, if compiling with optimization on.

I modified your code slightly to be able to use the online tool Compiler Explorer, changing the std::cout << x << std::endl to extern unsigned long foo; and foo = x;. Not really necessary but it makes the output cleaner.

Compiled with -O2:

test():
        movabs  rax, 2678554979246887653
        mov     QWORD PTR foo[rip], rax
        ret

Compiled with -O0:

test():
        push    rbp
        mov     rbp, rsp
        mov     QWORD PTR [rbp-8], 0
        mov     DWORD PTR [rbp-16], -1980424154
        mov     DWORD PTR [rbp-12], 0
        jmp     .L2
.L3:
        mov     rax, QWORD PTR [rbp-16]
        add     QWORD PTR [rbp-8], rax
        sub     QWORD PTR [rbp-16], 1
.L2:
        cmp     QWORD PTR [rbp-16], 0
        setne   al
        test    al, al
        jne     .L3
        mov     rax, QWORD PTR [rbp-8]
        mov     QWORD PTR foo[rip], rax
        leave
        ret

Also: the first revision of your code with undefined behavior due to i >= 0 just outputs:

test():
.L2:
        jmp     .L2

:-)

like image 84
simon Avatar answered Dec 21 '25 04:12

simon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!