In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "Stream performance test case analysis", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "Stream performance test case analysis" it!
Table of contents 1. Feedback
Q: stream is 5 times slower than for loop. What is the purpose of using this? A: the Internet is an era of rampant news, when three people become tigers and things happen when they are fake and real. As a technology developer, you should do it yourself and don't follow others' lead.
Indeed, I have also read this article mentioned by this fan, so I will not post the address, and there is no need to bring traffic to him. It's hard to say? It is a performance test done by a developer who does not know how to test, and gives an alarmist conclusion.
Second, all the performance test conclusions are one-sided
Performance testing is necessary, but always be skeptical about the results of performance tests. Why would you say that?
Performance testing away from the business scenario is one-sided performance testing. Can you cover all the business scenarios? Performance testing without the hardware environment is one-sided performance testing. Can you cover all the hardware environments? Performance testing is one-sided performance testing without developers' knowledge. Can you cover all kinds of developers' weird code?
Therefore, I never believe in any online performance testing articles. I have to test all my own business scenarios on machines close to the production environment. All the performance test conclusions are one-sided, and only the results of your production environment are true.
Third, hands-on test the performance of Stream 3.1. Environment
Windows10, 16G memory, i7-7700HQ 2.8HZ, 64-bit operating system, JDK 1.8.0x171
3.2. Test cases and test conclusions
We already talked about it in the last section:
For different data structures, the execution efficiency of stream is different for different data sources, and the execution efficiency of stream is also different.
So remember the author's words: all performance test conclusions are one-sided, you have to do it yourself, believe in your own code and the testing in your environment! My test results only represent my own test cases and test data structures!
3.2.1. Test case one
Test case: 500 million int random numbers to find the minimum test conclusion (see below for the test code):
Using a normal for loop, it is twice as efficient as a Stream serial flow. In other words, ordinary for loops have better performance. The Stream parallel flow meter is 4-5 times more efficient than the ordinary for loop. Stream parallel flow calculation > ordinary for cycle > Stream serial flow calculation
3.2. Test case two
Test case: 1000000 random string with a length of 10 to find the minimum test conclusion (see below for the test code):
The execution efficiency of ordinary for cycle is comparable to that of Stream serial flow. The execution efficiency of Stream parallel flow is much higher than that of ordinary for cycle Stream parallel flow calculation > ordinary for cycle = Stream serial flow calculation.
3.3. Test use case 3
Test case: 10 users, 200 orders per person. Count the total price of the order by user. Test conclusion (see below for the test code):
The execution efficiency of Stream parallel flow is much higher than that of ordinary for cycle Stream serial flow. The execution efficiency is greater than that of ordinary for cycle Stream parallel flow calculation > Stream serial flow calculation > = ordinary for loop 4. The final test conclusion: for simple list-Int traversal, the execution efficiency of ordinary for cycle is indeed higher than that of Stream serial flow. However, Stream streams can take advantage of the multi-core advantages of CPU by means of parallel execution, so the execution efficiency of parallel flows is higher than that of for cycles. For data traversal of list-Object type, there is no advantage between ordinary for loop and Stream serial flow ratio, let alone Stream parallel flow calculation.
Although in different scenarios, different data structures, different hardware environments. The performance test results of Stream flow and for cycle are quite different, and even reversed. But in general:
Stream parallel flow Computing > > ordinary for Loop ~ = Stream Serial flow Computing (the reason for using two greater than signs, you detail) the larger the data capacity, the higher the execution efficiency of Stream stream. Stream parallel flow computing can usually make good use of the multi-core advantages of CPU. The more CPU cores, the higher the computational efficiency of Stream parallel flow.
Stream cycle is 5 times slower than for cycle. Maybe, single-core CPU, serial Stream int type data traversal? I haven't tried this scenario, but I know it's not the core scenario of the application system. Read more than a dozen test posts, and my test results. My conclusion is that in most core business scenarios and common data structures, Stream performs more efficiently than for loops. After all, we usually have real entity objects in our business, so who always traverses the List type? Whose production server is single core?
5. Test code com.github.houbb junitperf 2.0.0
Test case one:
Import com.github.houbb.junitperf.core.annotation.JunitPerfConfig;import com.github.houbb.junitperf.core.report.impl.HtmlReporter;import org.junit.jupiter.api.BeforeAll;import java.util.Arrays;import java.util.Random;public class StreamIntTest {public static int [] arr; @ BeforeAll public static void init () {arr = new int [500000000]; / / 500m random Int randomInt (arr) } @ JunitPerfConfig (warmUp = 1000, reporter = {HtmlReporter.class}) public void testIntFor () {minIntFor (arr);} @ JunitPerfConfig (warmUp = 1000, reporter = {HtmlReporter.class}) public void testIntParallelStream () {minIntParallelStream (arr);} @ JunitPerfConfig (warmUp = 1000, reporter = {HtmlReporter.class}) public void testIntStream () {minIntStream (arr) } private int minIntStream (int [] arr) {return Arrays.stream (arr). Min (). GetAsInt ();} private int minIntParallelStream (int [] arr) {return Arrays.stream (arr). Parallel (). Min (). GetAsInt ();} private int minIntFor (int [] arr) {int min = Integer.MAX_VALUE; for (int anArr: arr) {if (anArr)
< min) { min = anArr; } } return min; } private static void randomInt(int[] arr) { Random r = new Random(); for (int i = 0; i < arr.length; i++) { arr[i] = r.nextInt(); } }} 测试用例二: import com.github.houbb.junitperf.core.annotation.JunitPerfConfig;import com.github.houbb.junitperf.core.report.impl.HtmlReporter;import org.junit.jupiter.api.BeforeAll;import java.util.ArrayList;import java.util.Random;public class StreamStringTest { public static ArrayList list; @BeforeAll public static void init() { list = randomStringList(1000000); } @JunitPerfConfig(duration = 10000, warmUp = 1000, reporter = {HtmlReporter.class}) public void testMinStringForLoop(){ String minStr = null; boolean first = true; for(String str : list){ if(first){ first = false; minStr = str; } if(minStr.compareTo(str)>0) {minStr = str;}} @ JunitPerfConfig (duration = 10000, warmUp = 1000, reporter = {HtmlReporter.class}) public void textMinStringStream () {list.stream () .min (String::compareTo) .get () } @ JunitPerfConfig (duration = 10000, warmUp = 1000, reporter = {HtmlReporter.class}) public void testMinStringParallelStream () {list.stream (). Parallel (). Min (String::compareTo). Get ();} private static ArrayList randomStringList (int listLength) {ArrayList list = new ArrayList (listLength); Random rand = new Random (); int strLength = 10; StringBuilder buf = new StringBuilder (strLength); for (int iTun0; 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.