Comparison between the efficiency of 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 NumbersM/= 2
It should be rewrittenM * = 0.5So I did the following two tests:

C code:

# Include <stdio. h> # include <time. h> main () {Long Count = 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: average in600 Ms

When we test the codeTest/= 2Change
Test x = 0.5
Hour

Test results: average value:520 Ms

Meets our expectations

Let's take a look at the same code running on 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/= 2Time consumed300 ms
Test x = 0.5Time consumed468 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:

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 = 1Time consumed86 Ms

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

Test results:

Test = 1Time consumed172 Ms

Nana! Time doubled!

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

Both results are in350 msLeft and Right, no change! Tangle!

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 codeTest % = 2;

Test results:

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

Java test results:

Test % = 2Time consumed1084 Ms
Test = 1Time consumed86 Ms

This is actually a very good reason. vc6.0 optimized touch 2 into bitwise operations, so I setTest % = 2ChangeTest % = 3;

Test Results

Test % = 3Time consumed1164 Ms, Verified my ideas. (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:

# 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 % = 3Time consumed92 ms

So I tested T.Est % = 7 ,... 31, All prime numbers, the same result!

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.3000 MsIt is three times the modulo time for other data;

In all the modulo operationsTest % =-1But it is the least time-consuming. How can we explain it in two cases?

6. C-free all operations or not do almost the same efficiency100 msLeft and Right, how to do it?

If you can help the blogger solve the above problems, please leave a message, or if you encounter incredible operator efficiency, please leave a message. The blogger hopes to discuss with you and improve your blog.

========================================================== ========================================================== ============================

Author: Nash _ Welcome to repost. sharing with others is the source of progress!

Reprinted Please retain the original address: http://blog.csdn.net/nash_/article/details/8223162

========================================================== ========================================================== ==============================

Related Article

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.