Difference in machine language code

Was wondering if the 1st code snippet would produce more efficient executable code than the 2nd:

(1) int main()
{ int n,i=1;
cout << "Enter a positive integer: ";
cin >> n;
long sum=0;
while (i <= n)
sum += i++;
cout << "The sum of the first " << n << " integers is " << sum;
}

(2) int main()
{ int n,i=1;
cout << "Enter a positive integer: ";
cin >> n;
long sum=0;
while (i <= n)
{
sum += i;
++i;
}
cout << "The sum of the first " << n << " integers is " << sum;
}
Time the programs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <chrono>

struct Timer
{
    Timer() : beg_(clock_::now()) {}
    void reset() { beg_ = clock_::now(); }
    double elapsed() const {
        return std::chrono::duration_cast<second_>
            (clock_::now() - beg_).count(); }

    using clock_= std::chrono::high_resolution_clock;
    using second_= std::chrono::duration<double, std::ratio<1>>;
    std::chrono::time_point<clock_> beg_;
};
int main()
{

    unsigned int n, i = 1, j = 1;
    std::cout << "Enter a positive integer: ";
    std::cin >> n;
    long sum1 = 0;
    Timer tmr;
    while (i <= n)
    {
        sum1 += i++;
    }
    double t = tmr.elapsed();

    std::cout << sum1 << '\n';
    std::cout << "Post-increment: " << t << '\n';
    sum1 = 0;

    tmr.reset();
    while (j <= n)
    {
        sum1 += ++j;
    }
    t = tmr.elapsed();
    std::cout << sum1 << '\n';
    std::cout << "Pre-increment: " << t << '\n';

    return 0;
}


Last edited on
Was wondering if the 1st code snippet would produce more efficient executable code than the 2nd:

I would be rather surprised to encounter an optimizing compiler that produced different code for your two snippets.
1
2
3
4
5
int main()
{ 
   	int n{}, i = 1, sum{};
	while (i <= n) sum += i++;
}

produces this machine code in GCC x86-64
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
main:
        push    rbp
        mov     rbp, rsp
        mov     DWORD PTR [rbp-12], 0
        mov     DWORD PTR [rbp-4], 1
        mov     DWORD PTR [rbp-8], 0
.L3:
        mov     eax, DWORD PTR [rbp-4]
        cmp     eax, DWORD PTR [rbp-12]
        jg      .L2
        mov     eax, DWORD PTR [rbp-4]
        lea     edx, [rax+1]
        mov     DWORD PTR [rbp-4], edx
        add     DWORD PTR [rbp-8], eax
        jmp     .L3
.L2:
        mov     eax, 0
        pop     rbp
        ret

https://godbolt.org/g/ASpHWG

While
1
2
3
4
5
6
7
8
int main()
{ 
   	int n{}, i = 1, sum{};
	while (i <= n) {
		sum += i;
		++i;
	} 
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
main:
        push    rbp
        mov     rbp, rsp
        mov     DWORD PTR [rbp-12], 0
        mov     DWORD PTR [rbp-4], 1
        mov     DWORD PTR [rbp-8], 0
.L3:
        mov     eax, DWORD PTR [rbp-4]
        cmp     eax, DWORD PTR [rbp-12]
        jg      .L2
        mov     eax, DWORD PTR [rbp-4]
        add     DWORD PTR [rbp-8], eax
        add     DWORD PTR [rbp-4], 1
        jmp     .L3
.L2:
        mov     eax, 0
        pop     rbp
        ret

https://godbolt.org/g/fYdFWh
In response to:
I would be rather surprised to encounter an optimizing compiler that produced different code for your two snippets.
integralfx wrote:
produces this machine code in GCC x86-64


Note that neither of those listings was produced with optimization enabled.

Here are the listings with optimization enabled:

First:
1
2
3
4
// https://godbolt.org/g/haHmMa
main:
        xor     eax, eax
        ret


Second:
1
2
3
4
// https://godbolt.org/g/haHmMa
main:
        xor     eax, eax
        ret


Makes a bit of a difference.
Topic archived. No new replies allowed.