Comparing the efficiency of the operators in C and JAVA cannot be explained at six points!

Source: Internet
Author: User

As we all know, the efficiency of operators in programming languages is quite different. Today, bloggers have tested the efficiency of value assignment operations, addition, subtraction, multiplication, multiplication, modulo and bitwise operations in C and JAVA respectively, I have produced some test results that I cannot explain. I will record the problems in this article. I hope they can be solved in the future. Please help me, next, let's list the problems I have tested.

Test environment: CPU: i3-370M; Memory: 4G; java ide: elipse; c ide: VC6.0, C-Free5.0.


Question 1: Is there a high multiplication efficiency or a high division efficiency?
Some books record multiplication efficiency is higher than division efficiency, for example, for floating point m/= 2 should be rewritten to m * = 0.5, so I did the following two tests:

C code:

[Cpp]
# Include <stdio. h>
# Include <time. h>
 
Main (){
Longcount = 100000000;
Float test = 1;
Int start_time, end_time;

Start_time = clock ();
While (-- count> 0)
Test/= 2;
End_time = clock ();

Printf ("test/= 2 elapsed time % d millisecond \ n", (end_time-start_time ));
 
Getchar ();
}
Test results: the average value is 600 ms.
When we change the test code test/= 2 to test * = 0.5

Test results: the average value is 520 ms.

Meets our expectations


Let's take a look at the same code running on JAVA:

[Java]
Public class FuHaoCeshi {
 
Public static void main (String [] args ){
Int count = 100000000;
Long start_time = 0;
Long end_time = 0;
Float test = 1;
Start_time = System. currentTimeMillis ();

While (-- count> 0)
Test/= 2;
End_time = System. currentTimeMillis ();
System. out. println ("test/= 2 time consumed" + (end_time-start_time) + "ms ");
 
Count = 100000000;
Start_time = System. currentTimeMillis ();
While (-- count> 0)
Test * = 0.5;
End_time = System. currentTimeMillis ();
System. out. println ("test * = 0.5 elapsed time" + (end_time-start_time) + "ms ");


}
 

}
Test results:
Test/= 2 time consumed 300 ms
Test * = 0.5 elapsed time 468 ms

The results are totally different from those in the VC test! Division efficiency is even higher --!


Question 2: Will a long integer double the time?
Let's change the test code in the JAVA program to the value assignment operation:

[Java]
Public class FuHaoCeshi {
 
Public static void main (String [] args ){
Int count = 100000000;
Long start_time = 0;
Long end_time = 0;
Int test = 1;
Start_time = System. currentTimeMillis ();

While (-- count> 0)
Test = 1;
End_time = System. currentTimeMillis ();
System. out. println ("test = 1 time consumed" + (end_time-start_time) + "ms ");
 


}
 

}
Test results:
Test = 1 time consumption 86 ms

Next we will change int count in the above program to long count to see the result:

Test results:

Test = 1 takes 172 ms

Nana! Time doubled!


Next, use the same code to test it in C language:

The two results are about ms and remain unchanged! Tangle!

According to the xuchao1229 reminder, it takes 170 ms to change long to double, but the life-and-death cycle is generated after changing it to float. How can this problem be explained?

Question 3: Is division and modulo operation overhead really high?
The modulo operation is implemented by Division. The overhead of division and modulo operation is large. So I changed the above test code to test % = 2;

Test results:

Test % = 2 takes 300 ms. (The time consumption is the same as that of test = 1 .)

Java test results:

Test % = 2 elapsed time 1084 ms
Test = 1 time consumption 86 ms
This is actually a very good reason. VC6.0 optimized touch 2 into bitwise operations, so I changed test % = 2 to test % = 3;

Test Results

Test % = 3 took 1164 ms to verify my idea. (JAVA does not change in time consumption and does not seem to have been optimized)

But this is not the point. The point is that I tested the same code with C-Free:

[Cpp]
# Include <stdio. h>
# Include <time. h>
 
Main (){
Int count = 100000000;
Int test = 1;
Int start_time, end_time;


Start_time = clock ();
While (-- count> 0)
Test % = 3;
End_time = clock ();

Printf ("Time consumed % d millisecond \ n", (end_time-start_time ));
Getchar ();
}
Test results:
Test % = 3 time consumed 92 ms

So I tested test % = 7 ,... 31. All prime numbers. The results are the same !!

How is the compiler optimized to the same time consumption? The same CPU, the processors in it, the multiplier, And the divisor do the same thing. Why?


There are several other minor issues below:
4. There is no average time consumption for the previous program loop count times: VC: 320 ms; C-Free: 92 ms; JVM: 85 ms,

Why C is the most efficient advanced language for execution, and JAVA is a language with a slow execution efficiency because of the JVM layer?

5. test % = 1 is the most time-consuming operation of JAVA tested in various aspects. The above Program reaches 3000 ms, which is three times the time-consuming for modulo other data;

Test % =-1 is the least time-consuming in all modulo operations. How can we explain the two cases?

6. C-Free all operations or not do almost the same efficiency in about ms, how to do it?

Www.2cto.com
If you can help the blogger solve the above problems, please leave a message, make a friend, or you have encountered incredible operator efficiency, please also leave a message, the blogger hopes to discuss and improve his blog.


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.