0

I have written a simple arithmetic logic in both C and JAVA. But C takes nearly 23.4s whereas JAVA takes around 4s to finish executing. This question is not based on how I calculate time which I suppose is already mentioned in the code. This is based on the execution. C code is as follows.

#include<stdio.h>
#include<time.h>

main() {
    clock_t begin = clock();
    long i, temp;
    for(i = 0; i<10000000000; i++)
        temp = i * 5;
     printf("temp : %ld\n", temp);
     clock_t end = clock();
     printf("time : %lf\n", (double) (end - begin) / CLOCKS_PER_SEC);
}

The output for C is

temp : 49999999995
time : 23.477688

JAVA code is as follows

public class Test {
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        long num = 5, temp = 0;
        for(long i = 0; i < 10000000000L; i++)
            temp = num * i;

        System.out.println(temp);
        long endTime   = System.currentTimeMillis();
        long totalTime = endTime - startTime;
        System.out.println("Execution time : " + totalTime);
    }
}

The output for JAVA is

49999999995
Execution time : 4194

Am I missing something to interpret JAVA is more efficient than C

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • 1
    Does that include the two minutes runtime warmup? – Martin James Aug 20 '17 at 10:00
  • @Martin what is runtime warmup? – Sridhar Ramanathan Aug 20 '17 at 10:04
  • 3
    are you sure your java code actually executes the loop? You only need the result of the very last iteration. Compiling the C code with optimizations would *very likely* eliminate the loop. –  Aug 20 '17 at 10:07
  • @Felix yea that is a valid suggestion, I am using GCC 6.3.0 and Java 8u144 running debian 9. Can we interpret GCC never applies optimization on its own? – Sridhar Ramanathan Aug 20 '17 at 10:10
  • 2
    GCC only applies very basic optimizations when not explicitly told to optimize more aggressively. Try `-O3` flag. For details, see [the manual](https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html). –  Aug 20 '17 at 10:11
  • OT: "*`printf("time : %lf\n", (double) ...`*": To print a `double` use the conversion specifier `f` only. The length modifier `l` isn't defined for `f`. (A `float` is promoted to `double` when being passed to a variadic function like `printf()` is.) – alk Aug 20 '17 at 10:20
  • I tried on my machine with JDK 1.8.0_144, 32-bits on Windows and it is much longer in Java than in your test. It takes about 20 seconds. Very close to the time you get with C code. Have you used a specific flag when you compile or run ? – davidxxx Aug 20 '17 at 10:21
  • That is a classical example why trivial programme should not be used for any performance measurements – 0___________ Aug 20 '17 at 10:25
  • @Sridhar Ramanathan this program with optimisations will take probably some us to execute – 0___________ Aug 20 '17 at 10:27
  • @alk after receiving some comments on the other question with this issue, I found C99 added a sentence stating that `l` is just ignored in `printf()` for `f` and friends. I don't know why I missed that checking the first time. So from C99 on, you **can** write `%lf` -- it's just unnecessary :) –  Aug 20 '17 at 10:28
  • My GCC 7.1.0 with `-O3` seems to compile this program to [an infinite loop](http://i.imgur.com/K53SqiG.png)! – tambre Aug 20 '17 at 10:29
  • 1
    It takes `0.000051` for me with `-03` optimization with my GCC – mjlowky Aug 20 '17 at 10:32
  • compiled with "gcc -o3 -o test.out test.c" the output is the same 23.452667s – Sridhar Ramanathan Aug 20 '17 at 10:35
  • 1
    @SridharRamanathan The compile option is `-O3`, **not** `-o3`. – tambre Aug 20 '17 at 10:36
  • That said, GCC can very easily [optimize it](https://godbolt.org/g/6ZhVp5) to basically nothing. You're doing something wrong. – tambre Aug 20 '17 at 10:37
  • @tambre please check this link https://stackoverflow.com/questions/1778538/how-many-gcc-optimization-levels-are-there is it O or 0 – Sridhar Ramanathan Aug 20 '17 at 11:00
  • @FelixPalmen Thanks a lot after applying optimization the execution time has really gone down to nothing . Thank you – Sridhar Ramanathan Aug 20 '17 at 11:04
  • 1
    Timing code doing nothing is useful for exactly what the code does. Nothing. – maaartinus Aug 20 '17 at 18:27

1 Answers1

-2

The difference is in the compilation of Java and C. The loop calculation could be calculated at run time or at compile time depending on compiler optimization. The compiler compiles to assembly code and the compiler could generate an assembly with the final result of the loop. For example, your code in x86-64 gcc 7.2 compiler without optimization would generate the following assembly code

.LC0:
  .string "temp : %ld\n"
.LC2:
  .string "time : %lf\n"
main:
  push rbp
  mov rbp, rsp
  sub rsp, 32
  call clock
  mov QWORD PTR [rbp-24], rax
  mov QWORD PTR [rbp-8], 0
.L3:
  movabs rax, 9999999999
  cmp QWORD PTR [rbp-8], rax
  jg .L2
  mov rdx, QWORD PTR [rbp-8]
  mov rax, rdx
  sal rax, 2
  add rax, rdx
  mov QWORD PTR [rbp-16], rax
  add QWORD PTR [rbp-8], 1
  jmp .L3
.L2:
  mov rax, QWORD PTR [rbp-16]
  mov rsi, rax
  mov edi, OFFSET FLAT:.LC0
  mov eax, 0
  call printf
  call clock
  mov QWORD PTR [rbp-32], rax
  mov rax, QWORD PTR [rbp-32]
  sub rax, QWORD PTR [rbp-24]
  cvtsi2sd xmm0, rax
  movsd xmm1, QWORD PTR .LC1[rip]
  divsd xmm0, xmm1
  mov edi, OFFSET FLAT:.LC2
  mov eax, 1
  call printf
  mov eax, 0
  leave
  ret
.LC1:
  .long 0
  .long 1093567616

But if you compile with the highest optimization it would generate the following assembly code

.LC0:
  .string "temp : %ld\n"
.LC2:
  .string "time : %lf\n"
main:
  push rbx
  call clock
  movabs rsi, 49999999995
  mov rbx, rax
  mov edi, OFFSET FLAT:.LC0
  xor eax, eax
  call printf
  call clock
  pxor xmm0, xmm0
  sub rax, rbx
  mov edi, OFFSET FLAT:.LC2
  cvtsi2sdq xmm0, rax
  mov eax, 1
  mulsd xmm0, QWORD PTR .LC1[rip]
  call printf
  xor eax, eax
  pop rbx
  ret
.LC1:
  .long 2696277389
  .long 1051772663

You can see that here movabs rsi, 49999999995 compiler calculated the final result of the loop and put it in the register.

I used : https://godbolt.org/

A R
  • 1
  • 2
  • Downvoting, as while this answer is right, it makes no attempt to explain the problem better. The wording is also confusing, making the answer hard to understand. – tambre Aug 20 '17 at 10:23