-
Notifications
You must be signed in to change notification settings - Fork 208
/
SortingBenchmarks.kt
63 lines (50 loc) · 2.11 KB
/
SortingBenchmarks.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.example.benchmark
import androidx.benchmark.junit4.BenchmarkRule
import androidx.benchmark.junit4.measureRepeated
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.benchmark.ui.SortingAlgorithms
import com.example.benchmark.ui.isSorted
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.random.Random
@RunWith(AndroidJUnit4::class)
class SortingBenchmarks {
@get:Rule
val benchmarkRule = BenchmarkRule()
// [START benchmark_with_timing_disabled]
// using random with the same seed, so that it generates the same data every run
private val random = Random(0)
// create the array once and just copy it in benchmarks
private val unsorted = IntArray(10_000) { random.nextInt() }
@Test
fun benchmark_quickSort() {
// [START_EXCLUDE]
// creating the variable outside of the measureRepeated to be able to assert after done
var listToSort = intArrayOf()
// [END_EXCLUDE]
benchmarkRule.measureRepeated {
// copy the array with timing disabled to measure only the algorithm itself
listToSort = runWithTimingDisabled { unsorted.copyOf() }
// sort the array in place and measure how long it takes
SortingAlgorithms.quickSort(listToSort)
}
// assert only once not to add overhead to the benchmarks
assertTrue(listToSort.isSorted)
}
// [END benchmark_with_timing_disabled]
@Test
fun benchmark_bubbleSort() {
// creating the variable outside of the measureRepeated to be able to assert after done
var listToSort = intArrayOf()
benchmarkRule.measureRepeated {
// copy the array with timing disabled to measure only the algorithm itself
listToSort = runWithTimingDisabled { unsorted.copyOf() }
// sort the array in place and measure how long it takes
SortingAlgorithms.bubbleSort(listToSort)
}
// assert only once not to add overhead to the benchmarks
assertTrue(listToSort.isSorted)
}
}