Comparison between bitwise operation and multiplication and division operation.

Source: Internet
Author: User

Comparison between bitwise operation and multiplication and division operation.

Many bitwise operations exist in Java JDK. The purpose is to make the code run faster. Let's conduct an experiment to see how much computing time can be saved.


In the following code, the multiplication and division operations and bit operations are used respectively to perform 0.1 billion basic operations and print the time consumed.

/*** Compare the time consumption gap between multiplication and division operations and bit operations */public class TestBitOpr {public static void main (String [] args) {int j = 10; long start = System. currentTimeMillis (); for (int I = 0; I <10000000; I ++) {if (I % 2 = 0) {for (int k = 0; k <10; k ++) {j = j * 2 ;}} else {for (int k = 0; k <10; k ++) {j = j/2 ;}}long end = System. currentTimeMillis (); System. out. println ("j =" + j + "multiplication and division operation time:" + (end-start) + "ms"); j = 10; start = System. currentTimeMillis (); for (int I = 0; I <10000000; I ++) {if (I % 2 = 0) {for (int k = 0; k <10; k ++) {j = j <1 ;}} else {for (int k = 0; k <10; k ++) {j = j> 1 ;}} end = System. currentTimeMillis (); System. out. println ("j =" + j + "bit operation time:" + (end-start) + "ms ");}}


1. Execute the above Code in my notebook. The obtained time is as follows:

J = 10 multiplication and division operation time: 548msj = 10-bit operation time: 115 ms


2. Run this code on a mobile device. The obtained time is as follows:

J = 10 multiplication and division operation time: 3544msj = 10-bit operation time: 928 ms



Summary:

In the case of a large amount of computing, bit operations can greatly save CPU time. the time consumed by multiplication and division operations is 3.5 ~ of bit operations ~ 5 times.

The time consumed on mobile devices is 7 ~ 10 times, we can see that the gap between the CPU of a mobile device and the CPU of a computer is still large. The time savings on a mobile device also indicate that the battery can be used longer, therefore, better code needs to be optimized on mobile devices.


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.