Last active
July 26, 2020 15:07
-
-
Save doom369/1c3062fe289617053264b9117bdd3ef7 to your computer and use it in GitHub Desktop.
Micro optimizations in Java. String.equals benchmark
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
@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