Skip to content

Commit

Permalink
BigInt Test
Browse files Browse the repository at this point in the history
  • Loading branch information
xp-vit committed Feb 26, 2023
1 parent 37092b0 commit e42e441
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 11 deletions.
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);
}
}*/
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package com.patotski.performance.bigint;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.*;

import java.math.BigInteger;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

import static com.patotski.performance.utils.BenchmarkUtils.runBenchmark;

@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 BigIntValidationBenchmarks {
@State(Scope.Benchmark)
public static class BigIntState {
Expand All @@ -21,13 +27,11 @@ public static class BigIntState {

public Pattern pattern = Pattern.compile(stringPattern);

private boolean flag = true;
public String getString() {
if (flag) {
flag = false;
//5% of the time return invalid string
if (ThreadLocalRandom.current().nextInt(20)==1) {
return invalid;
} else {
flag = true;
return valid;
}
}
Expand All @@ -43,15 +47,15 @@ public String exceptionValidation(BigIntState state) {
return "valid";
}

@Benchmark
/* @Benchmark
public String regExpValidation(BigIntState state) {
String stringToParse = state.getString();
if (stringToParse.matches(state.stringPattern)) {
state.bigInteger = new BigInteger(stringToParse);
return "valid";
}
return "invalid";
}
}*/

@Benchmark
public String regExpCompiledPatternValidation(BigIntState state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
@State(Scope.Benchmark)
@BenchmarkMode({Mode.AverageTime})
@Warmup(iterations = 2, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 3, time = 3, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 3, time = 5, timeUnit = TimeUnit.SECONDS)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(1)
public class StreamVsLoopsBenchmark {
private List<Integer> list;

@Param({ "1000" })
@Param({ "100000" })
private int size;

@Setup
Expand All @@ -37,6 +37,15 @@ public int stream() {
.orElse(0);
}

@Benchmark
public int streamParallel() {
return list.stream()
.parallel()
.filter(i -> i == -1)
.findAny()
.orElse(0);
}

@Benchmark
public int loop() {
for (Integer i : list) {
Expand Down
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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public String stringConcat() {
return "" + ThreadLocalRandom.current().nextInt();
}

@Benchmark
public String stringBuilder() {
return new StringBuilder().append(ThreadLocalRandom.current().nextInt()).toString();
}

@Benchmark
public String stringValueOf() {
return String.valueOf(ThreadLocalRandom.current().nextInt());
Expand Down

0 comments on commit e42e441

Please sign in to comment.