📄 benchmarkframework.java
字号:
package org.sunflow.system;
import org.sunflow.system.UI.Module;
/**
* This class provides a very simple framework for running a BenchmarkTest
* kernel several times and time the results.
*/
public class BenchmarkFramework {
private Timer[] timers;
private int timeLimit; // time limit in seconds
public BenchmarkFramework(int iterations, int timeLimit) {
this.timeLimit = timeLimit;
timers = new Timer[iterations];
}
public void execute(BenchmarkTest test) {
// clear previous results
for (int i = 0; i < timers.length; i++)
timers[i] = null;
// loop for the specified number of iterations or until the time limit
long startTime = System.nanoTime();
for (int i = 0; i < timers.length && ((System.nanoTime() - startTime) / 1000000000) < timeLimit; i++) {
UI.printInfo(Module.BENCH, "Running iteration %d", (i + 1));
timers[i] = new Timer();
test.kernelBegin();
timers[i].start();
test.kernelMain();
timers[i].end();
test.kernelEnd();
}
// report stats
double avg = 0;
double min = Double.POSITIVE_INFINITY;
double max = Double.NEGATIVE_INFINITY;
int n = 0;
for (Timer t : timers) {
if (t == null)
break;
double s = t.seconds();
min = Math.min(min, s);
max = Math.max(max, s);
avg += s;
n++;
}
if (n == 0)
return;
avg /= n;
double stdDev = 0;
for (Timer t : timers) {
if (t == null)
break;
double s = t.seconds();
stdDev += (s - avg) * (s - avg);
}
stdDev = Math.sqrt(stdDev / n);
UI.printInfo(Module.BENCH, "Benchmark results:");
UI.printInfo(Module.BENCH, " * Iterations: %d", n);
UI.printInfo(Module.BENCH, " * Average: %s", Timer.toString(avg));
UI.printInfo(Module.BENCH, " * Fastest: %s", Timer.toString(min));
UI.printInfo(Module.BENCH, " * Longest: %s", Timer.toString(max));
UI.printInfo(Module.BENCH, " * Deviation: %s", Timer.toString(stdDev));
for (int i = 0; i < timers.length && timers[i] != null; i++)
UI.printDetailed(Module.BENCH, " * Iteration %d: %s", i + 1, timers[i]);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -