Skip to content

Instantly share code, notes, and snippets.

@yanchenko
Created August 4, 2013 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yanchenko/6151266 to your computer and use it in GitHub Desktop.
Save yanchenko/6151266 to your computer and use it in GitHub Desktop.
/**
* 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