Skip to content

Instantly share code, notes, and snippets.

View doom369's full-sized avatar
🛴
Working from home

Dmytro Dumanskiy doom369

🛴
Working from home
View GitHub Profile
public static void main(String[] args) {
double average = 0;
int count = 0;
for (int i = 0; i < 1_000_000; i++) {
average += enhance(10, 15, Scroll.REGULAR);
count++;
}
System.out.println("Average : " + average / count);
average = 0;
@doom369
doom369 / StringEquals.java
Last active July 26, 2020 15:07
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;
@doom369
doom369 / StringEqualsResults.txt
Last active July 26, 2020 15:12
Micro optmizations in Java. String.equals benchmark result
Benchmark (strParams) Mode Cnt Score Error Units
EmptyStringEquals.equalsPost avgt 10 4.240 ± 0.058 ns/op
EmptyStringEquals.nonNullAndIsEmpty avgt 10 2.985 ± 0.048 ns/op
EmptyStringEquals.preEquals avgt 10 4.243 ± 0.062 ns/op
EmptyStringEquals.equalsPost nonEmptyString avgt 10 3.522 ± 0.034 ns/op
EmptyStringEquals.nonNullAndIsEmpty nonEmptyString avgt 10 2.894 ± 0.048 ns/op
EmptyStringEquals.preEquals nonEmptyString avgt 10 3.672 ± 0.016 ns/op
@doom369
doom369 / StringCharEqual.java
Created July 26, 2020 18:10
МІкрооптимізації в Java. String equals
@BenchmarkMode(Mode.AverageTime)
@Fork(1)
@State(Scope.Thread)
@Warmup(iterations = 5, time = 1)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Measurement(iterations = 10, time = 1)
public class EqualChar {
@Param({"/", "/my/server/url"})
private String url;
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String aString = (String)anObject;
if (coder() == aString.coder()) {
return isLatin1() ? StringLatin1.equals(value, aString.value)
: StringUTF16.equals(value, aString.value);
}
Benchmark (url) Mode Cnt Score Error Units
EqualChar.equalsOptimized / avgt 10 3.806 ± 0.075 ns/op
EqualChar.equalsPost / avgt 10 5.706 ± 0.089 ns/op
EqualChar.equalsPre / avgt 10 5.960 ± 0.062 ns/op
EqualChar.equalsOptimized /my/server/url avgt 10 3.250 ± 0.204 ns/op
EqualChar.equalsPost /my/server/url avgt 10 3.464 ± 0.116 ns/op
EqualChar.equalsPre /my/server/url avgt 10 3.786 ± 0.027 ns/op
class User {
String email;
int age;
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(1)
@State(Scope.Thread)
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 10, time = 1)
public class Hashcode {
UserObjects userObjects;
UserIDEDefault userIDEDefault;
Benchmark Mode Cnt Score Error Units
Hashcode.apacheCommons avgt 10 3.427 ± 0.025 ns/op
Hashcode.defaultHashCode avgt 10 3.440 ± 0.028 ns/op
Hashcode.guava avgt 10 8.631 ± 0.098 ns/op
Hashcode.lombok avgt 10 3.502 ± 0.060 ns/op
Hashcode.objectsHashcode avgt 10 8.722 ± 0.127 ns/op
@BenchmarkMode(Mode.AverageTime)
@Fork(1)
@State(Scope.Thread)
@Warmup(iterations = 5, time = 1)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Measurement(iterations = 10, time = 1)
public class EqualsIgnoreCase {
@Param({"HELLO WORLD", "Hello World", "hello world", "otherParam"})
String strParams;