Test the read/write performance of the volatile variable.

Source: Internet
Author: User

Test the read/write performance of the volatile variable.

Test the Read and Write Performance of the volatile variable. The difference between the volatile variable and the non-volatile variable is that each read of the volatile variable needs to read the latest value from the primary memory, and each write needs to be written back to the primary memory; non-volatile variables allow read/write in the cache to speed up computing. Obviously, the advantage of using volatile is that in a multi-threaded environment, data update is not timely when different threads share some data. Note: For volatile variables, the operation object of each thread during reading and writing is the primary memory (rather than copying the variable from various cache devices ), instead of using volatile to ensure absolute thread security.

Obviously, using volatile variables is more costly than using non-volatile variables, and various cache devices cannot be used to accelerate computing. In the Android environment, what is the difference between the Read/Write Performance of volatile variables and non-volatile variables? Use a piece of code to test:

Private static int AA = 10; private static volatile int BB = 10; // test read: private void test37 () {new Thread () {public void run () {int a = 0; int rep = 2000000000; long time, time2; time = System. currentTimeMillis (); Log. I ("TEST_VOLATILE", "start read test... "); for (int I = 0; I <rep; ++ I) {a = AA;} time2 = System. currentTimeMillis (); Log. I ("TEST_VOLATILE", "finish read test:" + (time2-time); time = System. currentTimeMillis (); Log. I ("TEST_VOLATILE", "start read volatile test... "); for (int I = 0; I <rep; ++ I) {a = BB;} time2 = System. currentTimeMillis (); Log. I ("TEST_VOLATILE", "finish read volatile test:" + (time2-time); Log. I ("TEST_VOLATILE", "" + );}}. start () ;}// test write: private void test38 () {new Thread () {public void run () {int a = 0; int rep = 2000000000; long time, time2; time = System. currentTimeMillis (); Log. I ("TEST_VOLATILE", "start write test... "); for (int I = 0; I <rep; ++ I) {AA = I;} time2 = System. currentTimeMillis (); Log. I ("TEST_VOLATILE", "finish write test:" + (time2-time); time = System. currentTimeMillis (); Log. I ("TEST_VOLATILE", "start write volatile test... "); for (int I = 0; I <rep; ++ I) {BB = I;} time2 = System. currentTimeMillis (); Log. I ("TEST_VOLATILE", "finish write volatile test:" + (time2-time); Log. I ("TEST_VOLATILE", "" + AA + "," + BB );}}. start ();}

In the above test code, AA and BB are static variables. In the method area, BB is volatile. The test program reads and writes AA and BB 2 billion times respectively, which is time consuming. The running result is as follows (unit: milliseconds ):

Read:

Sequence Non-volatile Volatile
1 11300 60788
2 2814 34539
3 2814 34533
4 2811 35057
5 2810 34535
Average 4510 39890

Write:

Sequence Non-volatile Volatile
1 11733 46776
2 2174 45977
3 2394 45956
4 2220 46640
5 2231 47112
Average 4150 46492

The above test runs on Xiaomi 5, based on android 7.0

We can see that for 2 billion reads, the volatile variable is about 8.8 times the time consumed by the non-volatile variable.
For 2 billion write times, the volatile variable is about 11.2 times the time consumed by the non-volatile variable.

First, it is time-consuming to zoom in to 2 billion times later. In terms of one read/write, the difference between the two is no longer perceptible. Therefore, for android-side development, you don't have to worry about using volatile, which may cause performance degradation. Of course, if it is a computing-intensive task, such as some large-scale data processing on the server, it is necessary to consider the performance degradation caused by volatile.

In comparison, the non-volatile cache mechanism improves the write performance significantly.

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.