Skip to content

Instantly share code, notes, and snippets.

@mooman219
Created January 17, 2015 00:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mooman219/bebbdc047889c7cfe612 to your computer and use it in GitHub Desktop.
Save mooman219/bebbdc047889c7cfe612 to your computer and use it in GitHub Desktop.
Benchmark (arg) Mode Samples Score Error Units
o.s.MyBenchmark.baseline 1 avgt 50 5.346 ñ 0.033 ns/op
o.s.MyBenchmark.baseline 100 avgt 50 5.402 ñ 0.053 ns/op
o.s.MyBenchmark.baseline 10 avgt 50 5.409 ñ 0.067 ns/op
o.s.MyBenchmark.baseline 1000 avgt 50 5.409 ñ 0.057 ns/op
o.s.MyBenchmark.baseline 5 avgt 50 5.373 ñ 0.057 ns/op
o.s.MyBenchmark.mapTest 1 avgt 50 6.989 ñ 0.044 ns/op
o.s.MyBenchmark.mapTest 100 avgt 50 7.259 ñ 0.111 ns/op
o.s.MyBenchmark.mapTest 10 avgt 50 7.125 ñ 0.159 ns/op
o.s.MyBenchmark.mapTest 1000 avgt 50 7.674 ñ 0.048 ns/op
o.s.MyBenchmark.mapTest 5 avgt 50 6.180 ñ 0.170 ns/op
o.s.MyBenchmark.switchTest 1 avgt 50 3.714 ñ 0.047 ns/op
o.s.MyBenchmark.switchTest 100 avgt 50 3.784 ñ 0.146 ns/op
o.s.MyBenchmark.switchTest 10 avgt 50 3.602 ñ 0.016 ns/op
o.s.MyBenchmark.switchTest 1000 avgt 50 4.067 ñ 0.050 ns/op
o.s.MyBenchmark.switchTest 5 avgt 50 3.738 ñ 0.028 ns/op
Benchmark (arg) Mode Samples Score Error Units
o.s.MyBenchmark.baseline 4000 avgt 30 5.427 ñ 0.071 ns/op
o.s.MyBenchmark.baseline 5501 avgt 30 5.435 ñ 0.099 ns/op
o.s.MyBenchmark.baseline 1000 avgt 30 5.530 ñ 0.149 ns/op
o.s.MyBenchmark.baseline 3000 avgt 30 5.371 ñ 0.053 ns/op
o.s.MyBenchmark.baseline 6000 avgt 30 5.424 ñ 0.102 ns/op
o.s.MyBenchmark.mapTest 4000 avgt 30 7.535 ñ 0.034 ns/op
o.s.MyBenchmark.mapTest 5501 avgt 30 6.203 ñ 0.226 ns/op
o.s.MyBenchmark.mapTest 1000 avgt 30 7.679 ñ 0.178 ns/op
o.s.MyBenchmark.mapTest 3000 avgt 30 7.561 ñ 0.056 ns/op
o.s.MyBenchmark.mapTest 6000 avgt 30 14.155 ñ 0.049 ns/op
o.s.MyBenchmark.switchTest 4000 avgt 30 4.446 ñ 0.044 ns/op
o.s.MyBenchmark.switchTest 5501 avgt 30 4.421 ñ 0.024 ns/op
o.s.MyBenchmark.switchTest 1000 avgt 30 4.497 ñ 0.021 ns/op
o.s.MyBenchmark.switchTest 3000 avgt 30 3.604 ñ 0.027 ns/op
o.s.MyBenchmark.switchTest 6000 avgt 30 4.448 ñ 0.034 ns/op
/*
* Copyright (c) 2014, Oracle America, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Oracle nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.sample;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class MyBenchmark {
final HashMap<Integer, String> map = new HashMap<Integer, String>() {
{
put(1, "a");
put(10, "b");
put(100, "c");
put(1000, "d");
}
};
@Param({"1", "100", "10", "1000", "5"})
public Integer arg;
@Benchmark
public String baseline() {
return "a";
}
@Benchmark
public String switchTest() {
switch (arg) {
case 1:
return "a";
case 10:
return "b";
case 100:
return "c";
case 1000:
return "d";
default:
return null;
}
}
@Benchmark
public String mapTest() {
return map.get(arg);
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(MyBenchmark.class.getSimpleName())
.warmupIterations(5)
.measurementIterations(5)
.build();
new Runner(opt).run();
}
}
/*
* Copyright (c) 2014, Oracle America, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Oracle nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.sample;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class MyBenchmark {
final HashMap<Integer, String> map = new HashMap<Integer, String>() {
{
put(1, "a");
put(10, "b");
put(100, "c");
put(1000, "d");
put(2000, "e");
put(2500, "f");
put(3000, "g");
put(3500, "h");
put(4000, "i");
put(4500, "j");
put(5000, "k");
put(5500, "l");
put(6000, "m");
}
};
@Param({"4000", "5501", "1000", "3000", "6000"})
public Integer arg;
@Benchmark
public String baseline() {
return "a";
}
@Benchmark
public String switchTest() {
switch (arg) {
case 1:
return "a";
case 10:
return "b";
case 100:
return "c";
case 1000:
return "d";
case 2000:
return "e";
case 2500:
return "f";
case 3000:
return "g";
case 3500:
return "h";
case 4000:
return "i";
case 4500:
return "j";
case 5000:
return "k";
case 5500:
return "l";
case 6000:
return "m";
default:
return null;
}
}
@Benchmark
public String mapTest() {
return map.get(arg);
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(MyBenchmark.class.getSimpleName())
.warmupIterations(3)
.measurementIterations(3)
.build();
new Runner(opt).run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment