对两种方法进行基准测试导致第一个结果不准确
Benchmarking two methods lead to first result being inaccurate
我正在尝试针对此主要功能中的同一问题对两种不同的方法进行基准测试。但是,second 方法的第一个基准与其他结果有很大不同。
有时还有一个问题,对于 second 方法,经过的时间等于 0.0 ns,这对我来说很奇怪。
我缺少什么?
输出
Iterative Method - Elapsed time (µs)
27.151
26.895
21.773
28.432
26.127
==================================================
Formula method - Elapsed time (µs)
5.123
1.281
0.512
0.513
0.512
主要代码:
public static void main(String[] args)
{
int testSize = 5;
System.out.println("Iterative Method\t-\tElapsed time (µs)");
for (int i = 0; i < testSize; i++)
{
long startTime = System.nanoTime();
iterativeMethod();
long estimatedTime = System.nanoTime() - startTime;
System.out.println("\t\t\t\t" + estimatedTime/1000f);
}
System.out.println("==================================================");
System.out.println("Formula method\t\t-\tElapsed time (µs)");
for (int i = 0; i < testSize; i++)
{
long startTime = System.nanoTime();
int foo = SumDivisibleBy(3) + SumDivisibleBy(5) - SumDivisibleBy(15);
long estimatedTime = System.nanoTime() - startTime;
System.out.println("\t\t\t\t" + estimatedTime/1000f);
}
}
我正在尝试针对此主要功能中的同一问题对两种不同的方法进行基准测试。但是,second 方法的第一个基准与其他结果有很大不同。
有时还有一个问题,对于 second 方法,经过的时间等于 0.0 ns,这对我来说很奇怪。
我缺少什么?
输出
Iterative Method - Elapsed time (µs)
27.151
26.895
21.773
28.432
26.127
==================================================
Formula method - Elapsed time (µs)
5.123
1.281
0.512
0.513
0.512
主要代码:
public static void main(String[] args)
{
int testSize = 5;
System.out.println("Iterative Method\t-\tElapsed time (µs)");
for (int i = 0; i < testSize; i++)
{
long startTime = System.nanoTime();
iterativeMethod();
long estimatedTime = System.nanoTime() - startTime;
System.out.println("\t\t\t\t" + estimatedTime/1000f);
}
System.out.println("==================================================");
System.out.println("Formula method\t\t-\tElapsed time (µs)");
for (int i = 0; i < testSize; i++)
{
long startTime = System.nanoTime();
int foo = SumDivisibleBy(3) + SumDivisibleBy(5) - SumDivisibleBy(15);
long estimatedTime = System.nanoTime() - startTime;
System.out.println("\t\t\t\t" + estimatedTime/1000f);
}
}