C taking more time to execute than JAVA -


this question has answer here:

i have written simple arithmetic logic in both c , java. c takes 23.4s whereas java takes around 4s finish executing. question not based on how calculate time suppose mentioned in code. based on execution. c code follows.

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

the output c

temp : 49999999995 time : 23.477688 

java code follows

public class test {     public static void main(string[] args) {         long starttime = system.currenttimemillis();         long num = 5, temp = 0;         for(long = 0; < 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 java

49999999995 execution time : 4194 

am missing interpret java more efficient c

the difference in compilation of java , c. loop calculation calculated @ run time or @ compile time depending on compiler optimization. compiler compiles assembly code , compiler generate assembly final result of loop. example, code in x86-64 gcc 7.2 compiler without optimization generate 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 compile highest optimization generate 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 here movabs rsi, 49999999995 compiler calculated final result of loop , put in register.

i used : https://godbolt.org/


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -