Created
August 4, 2013 18:11
-
-
Save yanchenko/6151266 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright 2013 Alex Yanchenko | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package org.droidparts; | |
import static java.lang.String.format; | |
public class MicroBenchmark { | |
public static MeasureResult measure(Runnable test, int warmupRounds, | |
int benchmarkRounds) { | |
// all times in ns | |
long[] warmup = new long[warmupRounds]; | |
long[] benchmark = new long[benchmarkRounds]; | |
// | |
runAndRecord(warmup, test); | |
runAndRecord(benchmark, test); | |
// | |
MeasureResult result = new MeasureResult(); | |
result.warmupRounds = warmupRounds; | |
result.benchmarkRounds = benchmarkRounds; | |
for (long time : warmup) { | |
result.warmupTime += time; | |
} | |
long sumSquares = 0; | |
for (long time : benchmark) { | |
result.benchmarkTime += time; | |
sumSquares += time * time; | |
} | |
double avg = result.benchmarkTime / (double) benchmark.length; | |
result.benchmarkRoundAverageTime = result.benchmarkTime | |
/ benchmark.length; | |
result.benchmarkRoundStandardDeviation = (long) (Math.sqrt(sumSquares | |
/ (double) benchmark.length - avg * avg)); | |
// convert to ms | |
result.warmupTime = ns2ms(result.warmupTime); | |
result.benchmarkTime = ns2ms(result.benchmarkTime); | |
result.benchmarkRoundAverageTime = ns2ms(result.benchmarkRoundAverageTime); | |
result.benchmarkRoundStandardDeviation = ns2ms(result.benchmarkRoundStandardDeviation); | |
// | |
return result; | |
} | |
private static void runAndRecord(long[] times, Runnable test) { | |
for (int i = 0; i < times.length; i++) { | |
long start = System.nanoTime(); | |
test.run(); | |
times[i] = System.nanoTime() - start; | |
} | |
} | |
private static long ns2ms(long nanoseconds) { | |
return nanoseconds / 1000000; | |
} | |
public static class MeasureResult { | |
public int warmupRounds, benchmarkRounds; | |
// ms | |
public long warmupTime, benchmarkTime; | |
public long benchmarkRoundAverageTime, benchmarkRoundStandardDeviation; | |
public void describe(StringBuilder sb){ | |
sb.append(format("round: %dms [+-%d], measured %d of %d rounds;\n", | |
benchmarkRoundAverageTime, | |
benchmarkRoundStandardDeviation, benchmarkRounds, | |
(warmupRounds + benchmarkRounds))); | |
sb.append(format("time.total: %dms, time.warmup: %dms, time.bench: %dms\n", (warmupTime + benchmarkTime), warmupTime, benchmarkTime)); | |
} | |
@Override | |
public String toString() { | |
StringBuilder sb = new StringBuilder(); | |
describe(sb); | |
return sb.toString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment