Skip to content Skip to sidebar Skip to footer

Programmatically, How To Write An Unoptimized Code To Calculate The Sum Of 10 Integers?

In Java, C, or C++, how to write an unoptimized code to calculate the sum of 10 integers (from 0 to 9) programmatically? For example, I use the following code but it is seems that

Solution 1:

godbolt is a pretty cool online C++ compiler that conveniently shows assembly output. I tried gcc, clang and icc and all of them optimised 1 + 2 + 3... source code into a compile time result even at -O0, but if you code it like this:

// Type your code here, or load an example.
int main()
{
    register intsum = 1;
    sum += 2;
    sum += 3;
    sum += 4;
    sum += 5;
    sum += 6;
    sum += 7;
    sum += 8;
    sum += 9;
    returnsum;
}

...you can get output like this (GCC 5.3.0 in this case)...

main:
    pushq   %rbp
    movq    %rsp, %rbp
    pushq   %rbx
    movl    $1, %ebx
    addl    $2, %ebx
    addl    $3, %ebx
    addl    $4, %ebx
    addl    $5, %ebx
    addl    $6, %ebx
    addl    $7, %ebx
    addl    $8, %ebx
    addl    $9, %ebx
    movl    %ebx, %eax
    popq    %rbx
    popq    %rbp
    ret

Clearly each add is being done separately. This also avoids overheads of loop control which you'd have if you introduced a for loop with bounds only known at runtime.

Still, what does it mean? There's no guarantee that the compiler will produce similar assembly given the same number of runtime-known variables....

Solution 2:

You can do this in C pretty easily by just disabling optimisations when you compile. Take this C program:

#include<stdio.h>intmain(int argc, char *argv[]){
    int sum = 0;
    for (int i = 0; i < 10; i++) {
        sum += i;
    }
    printf("%d\n", sum);
    return0;
}

And compile with gcc -O0 program.c. -O0 tells GCC not to optimise your code. If you take a look at the outputted assembly (which you can do by adding -S as an argument to compilation), you can see that it is indeed summing the first 10 numbers:

...
LBB0_1:                                 ## =>This Inner Loop Header: Depth=1
    cmpl    $10, -24(%rbp)
    jge LBB0_4
## BB#2:                                ##   in Loop: Header=BB0_1 Depth=1
    movl    -24(%rbp), %eax
    addl    -20(%rbp), %eax
    movl    %eax, -20(%rbp)
## BB#3:                                ##   in Loop: Header=BB0_1 Depth=1
    movl    -24(%rbp), %eax
    addl    $1, %eax
    movl    %eax, -24(%rbp)
    jmp LBB0_1
...

You can see the jump at the end of the snippet back to the start of the loop, and the conditional jump at the start of the loop to check i < 10. If you take a look at the optimised assembly code (compiling with gcc -O3 -S program.c), you can see GCC unrolls the loop and calculates the sum at compile time:

...
movl    $45, %esi
...

I have no idea why you want to do this though.

Solution 3:

You could use an array (which is an object type in Java), and then in Java 8+ you could use IntStream.sum(). Something like,

int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
intsum = IntStream.of(arr).sum();

or

intsum = IntStream.range(0, 10).sum();

Solution 4:

What you want is volatile....

#include<stdio.h>intmain( void ){

    volatileint n = 0;

    n += 1; n += 2; n += 3; n += 4; n += 5;
    n += 6; n += 7; n += 8; n += 9; n += 10;
    printf("%d\n", n);
    return0;
}

[EDIT] The addition appears to take place in a register, but there are two moves for each add:

    movl    -4(%ebp), %eax
    addl    $2, %eax
    movl    %eax, -4(%ebp)

Post a Comment for "Programmatically, How To Write An Unoptimized Code To Calculate The Sum Of 10 Integers?"