package com.jmh;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class PS {
static List<Integer> nums = new ArrayList<>();
static {
Random r = new Random();
for (int i = 0; i < 1000; i++) nums.add(1000000 + r.nextInt(1000000));
}
static void foreach() {
nums.forEach(v->isPrime(v));
}
static void parallel() {
nums.parallelStream().forEach(PS::isPrime);
}
static boolean isPrime(int num) {
for(int i=2; i<=num/2; i++) {
if(num % i == 0) return false;
}
return true;
}
}
package com.jmh;
import org.openjdk.jmh.annotations.*;
import static org.junit.jupiter.api.Assertions.*;
//com.mashibing.jmh.PSTest.testForEach
public class PSTest {
@Benchmark
@Warmup(iterations = 1, time = 3)
@Fork(5)
@BenchmarkMode(Mode.Throughput)
@Measurement(iterations = 1, time = 3)
public void testForEach() {
PS.foreach();
}
}
在.idea/workspace.xml中修改如下:
对以上方法做性能测试。