-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
176 additions
and
11 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/jmh/java/com/patotski/performance/array/VectorVsArrayListBenchmark.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.patotski.performance.array; | ||
|
||
import com.patotski.performance.utils.BenchmarkUtils; | ||
import org.openjdk.jmh.annotations.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Vector; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@State(Scope.Benchmark) | ||
@BenchmarkMode({Mode.AverageTime}) | ||
@Warmup(iterations = 2, time = 1, timeUnit = TimeUnit.SECONDS) | ||
@Measurement(iterations = 3, time = 2, timeUnit = TimeUnit.SECONDS) | ||
@OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
@Fork(1) | ||
public class VectorVsArrayListBenchmark { | ||
|
||
@Param({ "10", "100", "1000", "10000"}) | ||
private int size; | ||
|
||
@Benchmark | ||
public Vector vector() { | ||
Vector<Integer> vector = new Vector<>(); | ||
for (int i = 0; i < size; i++) { | ||
vector.add(i); | ||
} | ||
return vector; | ||
} | ||
|
||
@Benchmark | ||
public List arrayList() { | ||
List<Integer> list = new ArrayList<>(); | ||
for (int i = 0; i < size; i++) { | ||
list.add(i); | ||
} | ||
return list; | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
BenchmarkUtils.runBenchmark(VectorVsArrayListBenchmark.class); | ||
/* Vector<Integer> vector = new Vector<>(); | ||
int capacity = 0; | ||
for(int i = 0; i < 1000_000; i++) { | ||
vector.add(i); | ||
if (vector.capacity() != capacity) { | ||
capacity = vector.capacity(); | ||
System.out.println(capacity); | ||
} | ||
} | ||
ArrayList<Integer> list = new ArrayList<>(); | ||
capacity = 0; | ||
for(int i = 0; i < 1000_000; i++) { | ||
list.add(i); | ||
if (vector.capacity() != capacity) { | ||
capacity = vector.capacity(); | ||
System.out.println(capacity); | ||
} | ||
}*/ | ||
} | ||
} |
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
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
40 changes: 40 additions & 0 deletions
40
src/jmh/java/com/patotski/performance/exception/ExceptionReadOrNotBenchmark.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.patotski.performance.exception; | ||
|
||
import com.patotski.performance.utils.BenchmarkUtils; | ||
import org.openjdk.jmh.annotations.*; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@State(Scope.Benchmark) | ||
@BenchmarkMode({Mode.AverageTime}) | ||
@Warmup(iterations = 2, time = 1, timeUnit = TimeUnit.SECONDS) | ||
@Measurement(iterations = 3, time = 5, timeUnit = TimeUnit.SECONDS) | ||
@OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
@Fork(1) | ||
public class ExceptionReadOrNotBenchmark { | ||
@Benchmark | ||
public String readEx() { | ||
try { | ||
throw new NullPointerException("test"); | ||
} catch (NullPointerException e) { | ||
return e.getMessage(); | ||
} | ||
} | ||
|
||
@Benchmark | ||
public String ignoreEx() { | ||
try { | ||
throw new NullPointerException("test"); | ||
} catch (NullPointerException e) { | ||
return ""; | ||
} | ||
} | ||
|
||
private Integer getInteger() { | ||
return null; //ThreadLocalRandom.current().nextBoolean()? null : 1; | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
BenchmarkUtils.runBenchmark(ExceptionReadOrNotBenchmark.class); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/jmh/java/com/patotski/performance/exception/NullCheckVsExceptionBenchmark.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.patotski.performance.exception; | ||
|
||
import com.patotski.performance.utils.BenchmarkUtils; | ||
import org.openjdk.jmh.annotations.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@State(Scope.Benchmark) | ||
@BenchmarkMode({Mode.AverageTime}) | ||
@Warmup(iterations = 2, time = 1, timeUnit = TimeUnit.SECONDS) | ||
@Measurement(iterations = 3, time = 5, timeUnit = TimeUnit.SECONDS) | ||
@OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
@Fork(1) | ||
public class NullCheckVsExceptionBenchmark { | ||
@Benchmark | ||
public String nullCheck() { | ||
Integer i = getInteger(); | ||
if (i == null) { | ||
return "I is null."; | ||
} else { | ||
return i.toString(); | ||
} | ||
} | ||
|
||
@Benchmark | ||
public String npeExCatch() { | ||
Integer i = getInteger(); | ||
try { | ||
return i.toString(); | ||
} catch (NullPointerException e) { | ||
return e.getMessage(); | ||
} | ||
} | ||
|
||
private Integer getInteger() { | ||
return ThreadLocalRandom.current().nextBoolean()? null : 1; | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
BenchmarkUtils.runBenchmark(NullCheckVsExceptionBenchmark.class); | ||
} | ||
} |
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