為什么list.sort()比Stream().sorted()更快?
胖虎和朋友原創(chuàng)的視頻教程有興趣的可以看看:
(文末附課程大綱)
-
真的更好嗎? -
流本身的損耗 -
排序過程
list.sort()和list.strem().sorted()排序的差異。
list.sort()排序比stream().sorted()排序性能更好。
真的更好嗎?
List<Integer> userList = new ArrayList<>();
Random rand = new Random();
for (int i = 0; i < 10000 ; i++) {
userList.add(rand.nextInt(1000));
}
List<Integer> userList2 = new ArrayList<>();
userList2.addAll(userList);
Long startTime1 = System.currentTimeMillis();
userList2.stream().sorted(Comparator.comparing(Integer::intValue)).collect(Collectors.toList());
System.out.println("stream.sort耗時(shí):"+(System.currentTimeMillis() - startTime1)+"ms");
Long startTime = System.currentTimeMillis();
userList.sort(Comparator.comparing(Integer::intValue));
System.out.println("List.sort()耗時(shí):"+(System.currentTimeMillis()-startTime)+"ms");
stream.sort耗時(shí):62ms
List.sort()耗時(shí):7ms
stream.sort。
List<Integer> userList = new ArrayList<>();
Random rand = new Random();
for (int i = 0; i < 10000 ; i++) {
userList.add(rand.nextInt(1000));
}
List<Integer> userList2 = new ArrayList<>();
userList2.addAll(userList);
Long startTime = System.currentTimeMillis();
userList.sort(Comparator.comparing(Integer::intValue));
System.out.println("List.sort()耗時(shí):"+(System.currentTimeMillis()-startTime)+"ms");
Long startTime1 = System.currentTimeMillis();
userList2.stream().sorted(Comparator.comparing(Integer::intValue)).collect(Collectors.toList());
System.out.println("stream.sort耗時(shí):"+(System.currentTimeMillis() - startTime1)+"ms");
List.sort()耗時(shí):68ms
stream.sort耗時(shí):13ms
Long startTime = System.currentTimeMillis();
...
System.currentTimeMillis() - startTime
? 基準(zhǔn)測(cè)試是指通過設(shè)計(jì)科學(xué)的測(cè)試方法、測(cè)試工具和測(cè)試系統(tǒng),實(shí)現(xiàn)對(duì)一類測(cè)試對(duì)象的某項(xiàng)性能指標(biāo)進(jìn)行定量的和可對(duì)比的測(cè)試。 ?
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Warmup(iterations = 2, time = 1)
@Measurement(iterations = 5, time = 5)
@Fork(1)
@State(Scope.Thread)
public class SortBenchmark {
@Param(value = {"100", "10000", "100000"})
private int operationSize;
private static List<Integer> arrayList;
public static void main(String[] args) throws RunnerException {
// 啟動(dòng)基準(zhǔn)測(cè)試
Options opt = new OptionsBuilder()
.include(SortBenchmark.class.getSimpleName())
.result("SortBenchmark.json")
.mode(Mode.All)
.resultFormat(ResultFormatType.JSON)
.build();
new Runner(opt).run();
}
@Setup
public void init() {
arrayList = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < operationSize; i++) {
arrayList.add(random.nextInt(10000));
}
}
@Benchmark
public void sort(Blackhole blackhole) {
arrayList.sort(Comparator.comparing(e -> e));
blackhole.consume(arrayList);
}
@Benchmark
public void streamSorted(Blackhole blackhole) {
arrayList = arrayList.stream().sorted(Comparator.comparing(e -> e)).collect(Collectors.toList());
blackhole.consume(arrayList);
}
}
list.sort()效率確實(shí)比stream().sorted()要好。
流本身的損耗
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Warmup(iterations = 2, time = 1)
@Measurement(iterations = 5, time = 5)
@Fork(1)
@State(Scope.Thread)
public class SortBenchmark3 {
@Param(value = {"100", "10000"})
private int operationSize; // 操作次數(shù)
private static List<Integer> arrayList;
public static void main(String[] args) throws RunnerException {
// 啟動(dòng)基準(zhǔn)測(cè)試
Options opt = new OptionsBuilder()
.include(SortBenchmark3.class.getSimpleName()) // 要導(dǎo)入的測(cè)試類
.result("SortBenchmark3.json")
.mode(Mode.All)
.resultFormat(ResultFormatType.JSON)
.build();
new Runner(opt).run(); // 執(zhí)行測(cè)試
}
@Setup
public void init() {
// 啟動(dòng)執(zhí)行事件
arrayList = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < operationSize; i++) {
arrayList.add(random.nextInt(10000));
}
}
@Benchmark
public void stream(Blackhole blackhole) {
arrayList.stream().collect(Collectors.toList());
blackhole.consume(arrayList);
}
@Benchmark
public void sort(Blackhole blackhole) {
arrayList.stream().sorted(Comparator.comparing(Integer::intValue)).collect(Collectors.toList());
blackhole.consume(arrayList);
}
}
排序過程
-
1 begin方法初始化一個(gè)數(shù)組。 -
2 accept 接收上游數(shù)據(jù)。 -
3 end 方法開始進(jìn)行排序。
stream()排序所需時(shí)間肯定是 > 原生排序時(shí)間。
list.sort()比Stream().sorted()更快。
-
本文說的 stream() 流指的是串行流,而不是并行流。 -
絕大多數(shù)場(chǎng)景下,幾百幾千幾萬的數(shù)據(jù),開心就好,怎么方便怎么用,沒有必要去計(jì)較這點(diǎn)性能差異。
![]()
胖虎聯(lián)合兩位大佬朋友,一位是知名培訓(xùn)機(jī)構(gòu)講師和科大訊飛架構(gòu),聯(lián)合打造了《Java架構(gòu)師成長(zhǎng)之路》的視頻教程。完全對(duì)標(biāo)外面2萬左右的培訓(xùn)課程。
除了基本的視頻教程之外,還提供了超詳細(xì)的課堂筆記,以及源碼等資料包..
課程階段:
Java核心 提升閱讀源碼的內(nèi)功心法 深入講解企業(yè)開發(fā)必備技術(shù)棧,夯實(shí)基礎(chǔ),為跳槽加薪增加籌碼
分布式架構(gòu)設(shè)計(jì)方法論。為學(xué)習(xí)分布式微服務(wù)做鋪墊 學(xué)習(xí)NetFilx公司產(chǎn)品,如Eureka、Hystrix、Zuul、Feign、Ribbon等,以及學(xué)習(xí)Spring Cloud Alibabba體系 微服務(wù)架構(gòu)下的性能優(yōu)化 中間件源碼剖析 元原生以及虛擬化技術(shù) 從0開始,項(xiàng)目實(shí)戰(zhàn) SpringCloud Alibaba電商項(xiàng)目
點(diǎn)擊下方超鏈接查看詳情
(或者點(diǎn)擊文末閱讀原文):
(點(diǎn)擊查看) 2023年,最新Java架構(gòu)師成長(zhǎng)之路 視頻教程!
以下是課程大綱,大家可以雙擊打開原圖查看
評(píng)論
圖片
表情


