Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand java8 stream performance

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to understand java8 stream performance". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's ideas to study and learn "how to understand java8 stream performance".

What is the performance of Java 8 Stream?

In the previous article, we introduced Stream-related API in Java 8. We mentioned that Stream API can greatly improve the productivity of Java programmers, allowing programmers to write efficient, clean, and concise code.

So, what is the performance of Stream API, and does the neatness of the code mean the loss of performance? In this article, we take a look at the performance of Stream API.

To ensure the authenticity and credibility of the test results, we run JVM in-server mode, and the test data is of the order of GB. The test machine uses a common commercial server, and the configuration is as follows:

OSCentOS 6.7 x86_64CPUIntel Xeon X5675, 12m Cache 3.06 GHz, 6 Cores 12 Threads memory 96GBJDKjava version 1.8.0mm 91, Java HotSpot (TM) 64-Bit Server VM test method and test data

Performance testing is not easy, and Java performance testing is more laborious, because virtual machines have a great impact on performance, and JVM affects performance in two ways:

The influence of GC. The behavior of GC is a very difficult piece of Java to control. To increase certainty, we manually specify the use of the CMS collector and use 10GB to fix the size of heap memory. Specific to the JVM parameter is-XX:+UseConcMarkSweepGC-Xms10G-Xmx10G

JIT (Just-In-Time) real-time compilation technology. Just-in-time compilation technology will compile the hot code into local code when JVM is running. During the test, we will warm up the program and trigger the real-time compilation of the test function. The relevant JVM parameter is-XX:CompileThreshold=10000.

The thread pool obtained by ForkJoinPool.commonPool () is used for Stream parallel execution, and we use Linux's taskset command to specify the number of cores available for JVM to control parallelism.

The test data is randomly generated by the program. In order to prevent the jitter caused by a test, the average time is calculated as the running time for 4 times.

Experiment 1 basic type iteration

Test content: find the minimum value in the integer array. Compare the external iteration performance of the for loop with the internal iteration performance of the Stream API.

Test program code:

/ * java-server-Xms10G-Xmx10G-XX:+PrintGCDetails *-XX:+UseConcMarkSweepGC-XX:CompileThreshold=1000 lee/IntTest * taskset-c 0-[0meme 1, 3 taskset 7] java. * @ author CarpenterLee * / public class IntTest {public static void main (String [] args) {new IntTest (). DoTest ();} public void doTest () {warmUp (); int [] lengths = {10000, 100000, 1000000, 10000000, 100000000, 1000000000} For (int length: lengths) {System.out.println (String.format ("- array length:% dmurmuri -", length)); int [] arr = new int [length]; randomInt (arr); int times = 4int min1 = 1nint min2 = 2poliint min3 = 3X long startTime;startTime = System.nanoTime (); for (int item0; I)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report