Skip to content

Instantly share code, notes, and snippets.

@lsamayoa
Created July 8, 2015 15:08
Show Gist options
  • Save lsamayoa/130dbcc86f99d1b8ee87 to your computer and use it in GitHub Desktop.
Save lsamayoa/130dbcc86f99d1b8ee87 to your computer and use it in GitHub Desktop.
Final Keyworkd advantage on single use variable
// # JMH1.10.1(released 13 days ago)
// # VM invoker:/Library/Java/JavaVirtualMachines/1.6.0_65-b14-462.jdk/Contents/Home/bin/java
// # VM options:-Didea.launcher.port=7536-Didea.launcher.bin.path=/Applications/IntelliJ-IDEA-14.app/Contents/bin-Dfile.encoding=UTF-8
// # Warmup:1 iterations,1 s each
// # Measurement:5 iterations,1 s each
// # Timeout:10 min per iteration
// # Threads:1 thread,will synchronize iterations
// # Benchmark mode:Average time,time/op
// # Benchmark:com.xoom.api.client.l10n.FinalKeywordBenchmark.MainBenchmark.finalBenchmark
//
// # Run progress:0.00%complete,ETA 00:00:12
// # Fork:1 of 1
// # Warmup Iteration 1:1940990.347ns/op
// Iteration 1:1803519.784ns/op
// Iteration 2:1787671.403ns/op
// Iteration 3:1759243.433ns/op
// Iteration 4:1818459.313ns/op
// Iteration 5:1839140.768ns/op
//
//
// Result"finalBenchmark":
// 1801606.940 ±(99.9%)116880.136ns/op[Average]
// (min,avg,max)=(1759243.433,1801606.940,1839140.768),stdev=30353.400
// CI(99.9%):[1684726.804,1918487.076](assumes normal distribution)
//
//
// # JMH1.10.1(released 13 days ago)
// # VM invoker:/Library/Java/JavaVirtualMachines/1.6.0_65-b14-462.jdk/Contents/Home/bin/java
// # VM options:-Didea.launcher.port=7536-Didea.launcher.bin.path=/Applications/IntelliJ-IDEA-14.app/Contents/bin-Dfile.encoding=UTF-8
// # Warmup:1 iterations,1 s each
// # Measurement:5 iterations,1 s each
// # Timeout:10 min per iteration
// # Threads:1 thread,will synchronize iterations
// # Benchmark mode:Average time,time/op
// # Benchmark:com.xoom.api.client.l10n.FinalKeywordBenchmark.MainBenchmark.notFinalBenchmark
//
// # Run progress:50.00%complete,ETA 00:00:07
// # Fork:1 of 1
// # Warmup Iteration 1:1945411.992ns/op
// Iteration 1:1827960.000ns/op
// Iteration 2:1872893.855ns/op
// Iteration 3:1871284.916ns/op
// Iteration 4:1934232.692ns/op
// Iteration 5:1842595.238ns/op
//
//
// Result"notFinalBenchmark":
// 1869793.340 ±(99.9%)157052.859ns/op[Average]
// (min,avg,max)=(1827960.000,1869793.340,1934232.692),stdev=40786.129
// CI(99.9%):[1712740.481,2026846.199](assumes normal distribution)
//
//
// # Run complete.Total time:00:00:15
//
// Benchmark Mode Cnt Score Error Units
// FinalKeywordBenchmark.MainBenchmark.finalBenchmark avgt 5 1801606.940 ± 116880.136ns/op
// FinalKeywordBenchmark.MainBenchmark.notFinalBenchmark avgt 5 1869793.340 ± 157052.859ns/op
//
// Process finished with exit code 0
package com.xoom.api.client.l10n;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.openjdk.jmh.runner.options.TimeValue;
import java.util.concurrent.TimeUnit;
/**
* Created by lsamayoa on 7/8/15.
*/
public class FinalKeywordBenchmark {
public static void main(String[] args) throws Exception {
Options opt = new OptionsBuilder()
// Specify which benchmarks to run.
// You can be more specific if you'd like to run only one benchmark per test.
.include(FinalKeywordBenchmark.class.getName() + ".*")
// Set the following options as needed
.mode(Mode.AverageTime)
.timeUnit(TimeUnit.NANOSECONDS)
// .warmupTime(TimeValue.seconds(1))
.warmupIterations(1)
.measurementTime(TimeValue.seconds(1))
.measurementIterations(5)
.threads(1)
.forks(1)
.shouldFailOnError(true)
.shouldDoGC(true)
//.jvmArgs("-XX:+UnlockDiagnosticVMOptions", "-XX:+PrintInlining")
//.addProfiler(WinPerfAsmProfiler.class)
.build();
new Runner(opt).run();
}
@State(Scope.Thread)
public static class MainBenchmark {
@Benchmark
public void finalBenchmark(){
for (int i = 0; i < 15000; i++){
final String string = "aslkfuhsdkufhadskfha";
string.getBytes();
final int integer = 1656522;
final int integer2 = integer+1;
String.valueOf(integer2);
final boolean bool = true;
boolean bool2 = !bool;
String.valueOf(bool2);
}
}
@Benchmark
public void notFinalBenchmark() {
for (int i = 0; i < 15000; i++) {
String string = "aslkfuhsdkufhadskfha";
string.getBytes();
int integer = 1656522;
final int integer2 = integer + 1;
String.valueOf(integer2);
boolean bool = true;
boolean bool2= !bool;
String.valueOf(bool2);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment