Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions examples/example-gradle/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,31 @@ version = "1.0-SNAPSHOT"

jmh {
jmhVersion.set("0.1.0")

benchmarkMode.set(listOf("avgt"))
timeUnit.set("ns")
warmupIterations.set(2)
warmup.set("1s")
iterations.set(3)
timeOnIteration.set("1s")
fork.set(3)
resultFormat.set("JSON")

// Force a System.gc() between iterations so heap state is reset before each
// measurement starts. Without this, a GC pause can land inside the measurement
// window and show up as a spurious regression in CI.
forceGC = true

// Run subset of the benchmarks to avoid long CI runtimes
if (System.getenv("CODSPEED_ENV") != null) {
logger.lifecycle("CODSPEED_ENV detected — running curated CI benchmark subset")
// me.champeau.jmh joins `includes` with commas into a single positional
// regex passed to JMH, so multiple entries collapse to one pattern with
// literal commas and match nothing. Use a single alternation instead.
includes.set(listOf(
".*(SleepBenchmark|BacktrackingBenchmark|FibBenchmark).*",
))
}
}

sourceSets {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
package bench;

import com.thealgorithms.backtracking.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Warmup(iterations = 1, time = 1)
@Measurement(iterations = 3, time = 1)
@Fork(1)
public class BacktrackingBenchmark {

// -- N-Queens --

@State(Scope.Benchmark)
public static class NQueensState {
@Param({"4", "5", "6", "7", "8"})
@Param({"8"})
public int nQueens;
}

Expand All @@ -30,7 +28,7 @@ public List<List<String>> nQueensSolver(NQueensState state) {

@State(Scope.Benchmark)
public static class ParenthesesState {
@Param({"3", "4", "5", "6"})
@Param({"6"})
public int nParens;
}

Expand All @@ -43,34 +41,46 @@ public List<String> generateParentheses(ParenthesesState state) {

@State(Scope.Benchmark)
public static class CombinationsState {
@Param({"5", "6", "7", "8", "9"})
@Param({"9"})
public int nCombinations;

public Integer[] arr;

@Setup(Level.Trial)
public void setup() {
arr = new Integer[nCombinations];
for (int i = 0; i < nCombinations; i++) {
arr[i] = i;
}
}
}

@Benchmark
public void generateCombinations(CombinationsState state, Blackhole bh) {
Integer[] arr = new Integer[state.nCombinations];
for (int i = 0; i < state.nCombinations; i++) {
arr[i] = i;
}
bh.consume(Combination.combination(arr, 3));
bh.consume(Combination.combination(state.arr, 3));
}

// -- Permutations --

@State(Scope.Benchmark)
public static class PermutationsState {
@Param({"3", "4", "5", "6", "7"})
@Param({"7"})
public int nPermutations;

public Integer[] arr;

@Setup(Level.Trial)
public void setup() {
arr = new Integer[nPermutations];
for (int i = 0; i < nPermutations; i++) {
arr[i] = i;
}
}
}

@Benchmark
public void permutations(PermutationsState state, Blackhole bh) {
Integer[] arr = new Integer[state.nPermutations];
for (int i = 0; i < state.nPermutations; i++) {
arr[i] = i;
}
bh.consume(Permutation.permutation(arr));
bh.consume(Permutation.permutation(state.arr));
}

// -- Sudoku Solver --
Expand Down Expand Up @@ -105,16 +115,22 @@ public boolean sudokuSolver(SudokuState state) {

@State(Scope.Benchmark)
public static class SubsequencesState {
@Param({"8", "10", "12"})
@Param({"12"})
public int nSubsequences;

public List<Integer> seq;

@Setup(Level.Trial)
public void setup() {
seq = new ArrayList<>(nSubsequences);
for (int i = 0; i < nSubsequences; i++) {
seq.add(i);
}
}
}

@Benchmark
public void generateSubsequences(SubsequencesState state, Blackhole bh) {
List<Integer> seq = new java.util.ArrayList<>();
for (int i = 0; i < state.nSubsequences; i++) {
seq.add(i);
}
bh.consume(SubsequenceFinder.generateAll(seq));
bh.consume(SubsequenceFinder.generateAll(state.seq));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
@Warmup(iterations = 1, time = 1)
@Measurement(iterations = 3, time = 1)
@Fork(1)
public class BitManipulationBenchmark {

@Param({"0", "42", "255", "1024", "65535"})
@Param({"65535"})
private int bitValue;

@Benchmark
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 1, time = 1)
@Measurement(iterations = 3, time = 1)
@Fork(1)
public class DynamicProgrammingBenchmark {

// -- Fibonacci --

@State(Scope.Benchmark)
public static class FibonacciState {
@Param({"10", "20", "30", "40"})
@Param({"30"})
public int fibN;
}

Expand All @@ -38,7 +35,7 @@ public int fibonacciOptimized(FibonacciState state) {

@State(Scope.Benchmark)
public static class KnapsackState {
@Param({"10", "15", "20"})
@Param({"20"})
public int knapsackSize;

public int[] knapsackWeights;
Expand All @@ -64,7 +61,7 @@ public int knapsack(KnapsackState state) {

@State(Scope.Benchmark)
public static class EditDistanceState {
@Param({"kitten", "saturday"})
@Param({"saturday"})
public String editWord1;
}

Expand All @@ -77,7 +74,7 @@ public int editDistance(EditDistanceState state) {

@State(Scope.Benchmark)
public static class LevenshteinState {
@Param({"kitten sitting", "saturday sunday"})
@Param({"saturday sunday"})
public String levenshteinPair;
}

Expand All @@ -91,7 +88,7 @@ public int levenshteinDistance(LevenshteinState state) {

@State(Scope.Benchmark)
public static class LisState {
@Param({"10", "50", "100"})
@Param({"100"})
public int lisSize;

public int[] lisArray;
Expand All @@ -115,7 +112,7 @@ public int longestIncreasingSubsequence(LisState state) {

@State(Scope.Benchmark)
public static class CoinChangeState {
@Param({"50", "100", "200"})
@Param({"200"})
public int coinAmount;
}

Expand All @@ -130,7 +127,7 @@ public int coinChange(CoinChangeState state) {

@State(Scope.Benchmark)
public static class SubsetSumState {
@Param({"10", "15", "20"})
@Param({"20"})
public int subsetSize;

public int[] subsetArr;
Expand Down
3 changes: 0 additions & 3 deletions examples/example-gradle/src/jmh/java/bench/FibBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
@Warmup(iterations = 1, time = 1)
@Measurement(iterations = 3, time = 1)
@Fork(1)
public class FibBenchmark {

@Param({"30"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)
@Warmup(iterations = 1, time = 1)
@Measurement(iterations = 3, time = 1)
@Fork(1)
public class RegexBenchmark {

// Complex pattern with alternations, named groups, lookahead, and quantifiers.
Expand All @@ -26,7 +23,7 @@ public class RegexBenchmark {
// Nested quantifiers: (a+)+ applied to a string of 'a's followed by a non-matching char.
private static final String BACKTRACK_PATTERN = "^(a+)+b$";

@Param({"20", "24"})
@Param({"24"})
private int backtrackLength;

private String scanInput;
Expand Down
5 changes: 1 addition & 4 deletions examples/example-gradle/src/jmh/java/bench/RleBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Benchmark)
@Warmup(iterations = 1, time = 1)
@Measurement(iterations = 3, time = 1)
@Fork(1)
public class RleBenchmark {

@Param({"1024", "65536"})
@Param({"65536"})
private int size;

private byte[] rawData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
@Warmup(iterations = 1, time = 1)
@Measurement(iterations = 3, time = 1)
@Fork(1)
public class SleepBenchmark {

private static void busyWait(long nanos) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,89 +1,57 @@
package com.thealgorithms.sorts;

import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.*;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)
@Warmup(iterations = 1, time = 1)
@Measurement(iterations = 3, time = 1)
@Fork(1)
public class SortBenchmark {

@Param({"100", "1000", "10000"})
@Param({"10000"})
private int size;

private Integer[] data;
private Integer[] working;

private final QuickSort quickSort = new QuickSort();
private final MergeSort mergeSort = new MergeSort();
private final HeapSort heapSort = new HeapSort();
private final InsertionSort insertionSort = new InsertionSort();
private final BubbleSort bubbleSort = new BubbleSort();
private final TimSort timSort = new TimSort();
private final ShellSort shellSort = new ShellSort();
private final SelectionSort selectionSort = new SelectionSort();
private final DualPivotQuickSort dualPivotQuickSort = new DualPivotQuickSort();
private final IntrospectiveSort introspectiveSort = new IntrospectiveSort();

@Setup(Level.Trial)
public void setup() {
Random rng = new Random(42);
data = new Integer[size];
working = new Integer[size];
for (int i = 0; i < size; i++) {
data[i] = rng.nextInt(size * 10);
}
}

private Integer[] copyData() {
return Arrays.copyOf(data, data.length);
private Integer[] resetWorking() {
System.arraycopy(data, 0, working, 0, size);
return working;
}

@Benchmark
public Integer[] quickSort() {
return quickSort.sort(copyData());
return quickSort.sort(resetWorking());
}

@Benchmark
public Integer[] mergeSort() {
return mergeSort.sort(copyData());
}

@Benchmark
public Integer[] heapSort() {
return heapSort.sort(copyData());
return mergeSort.sort(resetWorking());
}

@Benchmark
public Integer[] timSort() {
return timSort.sort(copyData());
}

@Benchmark
public Integer[] shellSort() {
return shellSort.sort(copyData());
}

@Benchmark
public Integer[] selectionSort() {
return selectionSort.sort(copyData());
}

@Benchmark
public Integer[] insertionSort() {
return insertionSort.sort(copyData());
return timSort.sort(resetWorking());
}

@Benchmark
public Integer[] dualPivotQuickSort() {
return dualPivotQuickSort.sort(copyData());
}

@Benchmark
public Integer[] introspectiveSort() {
return introspectiveSort.sort(copyData());
return dualPivotQuickSort.sort(resetWorking());
}
}
Loading
Loading