Skip to content

Commit 9ad73ae

Browse files
added proper benchmarking
1 parent ddfecb1 commit 9ad73ae

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/main/scala/algorithms/sort/InsertionSort.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ def insertionSortInPlace(arr: Array[Int]): Unit = {
2323
}
2424
}
2525

26+
def returnInsertionSort(arr: Array[Int]): Array[Int] = {
27+
insertionSortInPlace(arr)
28+
arr
29+
}
30+
2631
/// Sorting an integer array by insertion, returning a new List
2732
/// Input is immutable (lists are immutable in scala)
2833
/// Example

src/main/scala/benchmark/SortingBenchmark.scala

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
1-
/*
21
package benchmark
32

4-
import algorithms.sort.returnBubbleSort
5-
import algorithms.sort.heapSort
6-
7-
import org.openjdk.jmh.annotations.*
3+
import algorithms.sort.*
4+
import org.openjdk.jmh.annotations._
85

96
import java.util.concurrent.TimeUnit
10-
import scala.collection.mutable
117
import scala.util.Random
128

13-
// sbt jmh / run -i 5 -wi 2 -f1 -t1
149
@BenchmarkMode(Array(Mode.AverageTime))
1510
@OutputTimeUnit(TimeUnit.MILLISECONDS)
1611
@State(Scope.Thread)
17-
@Warmup(iterations = 2) // Reduce warm-up iterations (default is 5)
18-
@Measurement(iterations = 5) // Reduce total benchmark iterations (default is 10)
19-
@Fork(1) // Reduce forks (default is 3)
12+
@Warmup(iterations = 2)
13+
@Measurement(iterations = 5)
14+
@Fork(1)
2015
class SortingBenchmark {
2116

22-
var data: Array[Int] = new Array[Int](5000)
17+
var data: Array[Int] = new Array[Int](500) // Smaller dataset for quick benchmarking
2318

24-
@Setup(Level.Iteration) // Runs before each benchmark iteration
19+
@Setup(Level.Iteration)
2520
def prepare(): Unit = {
26-
data = Array.fill(5000)(Random.nextInt(1000000)) // Large random dataset
21+
data = Array.fill(500)(Random.nextInt(500)) // Smaller random dataset
2722
}
2823

2924
@Benchmark
@@ -35,5 +30,15 @@ class SortingBenchmark {
3530
def benchmarkHeapSort(): Array[Int] = {
3631
heapSort(data.clone())
3732
}
33+
34+
@Benchmark
35+
def benchmarkCountingSort(): Array[Int] = {
36+
countingSort(data.clone(), 500)
37+
}
38+
39+
@Benchmark
40+
def benchmarkInsertionSort(): Array[Int] = {
41+
returnInsertionSort(data.clone())
42+
}
43+
3844
}
39-
*/

0 commit comments

Comments
 (0)