Skip to content

Instantly share code, notes, and snippets.

@doom369
Last active July 26, 2020 15:07
Show Gist options
  • Save doom369/1c3062fe289617053264b9117bdd3ef7 to your computer and use it in GitHub Desktop.
Save doom369/1c3062fe289617053264b9117bdd3ef7 to your computer and use it in GitHub Desktop.
Micro optimizations in Java. String.equals benchmark
@BenchmarkMode(Mode.AverageTime)
@Fork(1)
@State(Scope.Thread)
@Warmup(iterations = 5, time = 1)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Measurement(iterations = 10, time = 1)
public class EmptyStringEquals {
@Param({"", "nonEmptyString"})
private String strParams;
@Benchmark
public boolean nonNullAndIsEmpty() {
return strParams != null && strParams.isEmpty();
}
@Benchmark
public boolean equalsPost() {
return strParams != null && strParams.equals("");
}
@Benchmark
public boolean preEquals() {
return "".equals(strParams);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment