因为会有性能损失。
测试代码:
1.Java 中直接使用 Java 8 的 Stream API:
代码语言:javascript复制package i;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* @author: Jack
* 2020-05-06 16:20
*/
public class StreamParallelDemo {
public static void main(String[] args) {
System.out.println(String.format("本计算机的核数:%d", Runtime.getRuntime().availableProcessors()));
Random random = new Random();
List<Integer> list = new ArrayList<>(100000000);
for (int i = 0; i < 100000000; i ) {
list.add(random.nextInt(100));
}
long s = getCurrentTime();
Integer ans = list.stream().reduce((a, b) -> a b).get();
System.out.println(ans);
System.out.println(String.format("单线程计算耗时:%d ms", getCurrentTime() - s));
s = getCurrentTime();
ans = list.stream().parallel().reduce((a, b) -> a b).get();
System.out.println(ans);
System.out.println(String.format("多线程计算耗时:%d ms", getCurrentTime() - s));
}
private static long getCurrentTime() {
return System.currentTimeMillis();
}
}
运行结果:
本计算机的核数:12 655269092 单线程计算耗时:3350 ms 655269092 多线程计算耗时:425 ms
2.Kotlin 使用 Java 8 的 Stream API:
代码语言:javascript复制package i
import java.util.*
/**
* @author: Jack
* 2020-05-06 16:03
*/
fun main() {
val n = 100000000
val random = Random()
val numbers = ArrayList<Int>(n)
for (i in 0..n - 1) {
numbers.add(random.nextInt(100))
}
run {
val s = System.currentTimeMillis()
val ans = singleSum(numbers)
val t = System.currentTimeMillis()
println(ans)
println("singleSum Time:${t - s}ms")
}
}
fun singleSum(numbers: List<Int>): Int {
return numbers.stream().reduce { t: Int, u: Int -> t u }.get()
}
运行结果:
655113317 singleSum Time:3509ms
代码语言:javascript复制package i
import java.util.*
/**
* @author: Jack
* 2020-05-06 16:03
*/
fun main() {
val n = 100000000
val random = Random()
val numbers = ArrayList<Int>(n)
for (i in 0..n - 1) {
numbers.add(random.nextInt(100))
}
run {
val s = System.currentTimeMillis()
val ans = parallelSum(numbers)
val t = System.currentTimeMillis()
println(ans)
println("parallelSum Time:${t - s}ms")
}
}
fun parallelSum(numbers: List<Int>): Int {
return numbers.stream().parallel().reduce { t: Int, u: Int -> t u }.get()
}
运行结果: 655149583 parallelSum Time:2911ms
看起来,Kotlin使用Java 8 的 parallel Stream API,似乎没有明显的性能提升(猜测:中间有Java-Kotlin互转带来的性能损耗?)。
3.直接使用Kotlin的 List的API:
代码语言:javascript复制package i
import java.util.*
/**
* @author: Jack
* 2020-05-06 16:03
*/
fun main() {
val n = 100000000
val random = Random()
val numbers = ArrayList<Int>(n)
for (i in 0..n - 1) {
numbers.add(random.nextInt(100))
}
run {
val s = System.currentTimeMillis()
val ans = listSum(numbers)
val t = System.currentTimeMillis()
println(ans)
println("listSum Time:${t - s}ms")
}
}
fun listSum(numbers: List<Int>): Int {
return numbers.stream().parallel().reduce { t: Int, u: Int -> t u }.get()
}
运行结果: 655344706 listSum Time:3235ms
4.使用Kotlin的Sequence API
代码语言:javascript复制package i
import java.util.*
/**
* @author: Jack
* 2020-05-06 16:03
*/
fun main() {
val n = 100000000
val random = Random()
val numbers = MutableList(n) { random.nextInt(100) }
run {
val s = System.currentTimeMillis()
val ans = sequenceSum(numbers)
val t = System.currentTimeMillis()
println(ans)
println("sequenceSum Time:${t - s}ms")
}
}
fun sequenceSum(numbers: List<Int>): Int {
return numbers.asSequence().reduce { t: Int, u: Int -> t u }
}
运行结果:
655104311 sequenceSum Time:3176ms
思考题
为什么会有性能损失呢?