11package com .baeldung .array ;
22
3- import org .junit . Test ;
3+ import org .openjdk . jmh . annotations .* ;
44
55import java .util .*;
6+ import java .util .concurrent .TimeUnit ;
67
8+ @ BenchmarkMode (Mode .AverageTime )
9+ @ Warmup (iterations = 5 )
10+ @ OutputTimeUnit (TimeUnit .MICROSECONDS )
711public class SearchArrayTest {
812
9-
10- @ Test
11- public void searchArrayAllocNewCollections () {
13+ @ Benchmark
14+ public void searchArrayLoop () {
1215
1316 int count = 1000 ;
14-
1517 String [] strings = seedArray (count );
16-
17- long startTime = System .nanoTime ();
1818 for (int i = 0 ; i < count ; i ++) {
19- searchList (strings , "W " );
19+ searchLoop (strings , "T " );
2020 }
21- long duration = System .nanoTime () - startTime ;
22- System .out .println ("SearchList: " + duration / 10000 );
21+ }
22+
23+ @ Benchmark
24+ public void searchArrayAllocNewList () {
2325
24- startTime = System .nanoTime ();
26+ int count = 1000 ;
27+ String [] strings = seedArray (count );
2528 for (int i = 0 ; i < count ; i ++) {
26- searchSet (strings ,"S " );
29+ searchList (strings , "W " );
2730 }
28- duration = System .nanoTime () - startTime ;
29- System .out .println ("SearchSet: " + duration / 10000 );
3031
31- startTime = System .nanoTime ();
32+ }
33+
34+ @ Benchmark
35+ public void searchArrayAllocNewSet () {
36+
37+ int count = 1000 ;
38+ String [] strings = seedArray (count );
3239 for (int i = 0 ; i < count ; i ++) {
33- searchLoop (strings , "T " );
40+ searchSet (strings , "S " );
3441 }
35- duration = System .nanoTime () - startTime ;
36- System .out .println ("SearchLoop: " + duration / 10000 );
3742 }
3843
39- @ Test
40- public void searchArrayReuseCollections () {
4144
42- int count = 10000 ;
45+ @ Benchmark
46+ public void searchArrayReuseList () {
47+
48+ int count = 1000 ;
4349 String [] strings = seedArray (count );
4450
4551 List <String > asList = Arrays .asList (strings );
46- Set <String > asSet = new HashSet <>(Arrays .asList (strings ));
4752
48- long startTime = System .nanoTime ();
4953 for (int i = 0 ; i < count ; i ++) {
5054 asList .contains ("W" );
5155 }
52- long duration = System .nanoTime () - startTime ;
53- System .out .println ("List: " + duration / 10000 );
56+ }
5457
55- startTime = System .nanoTime ();
56- for (int i = 0 ; i < count ; i ++) {
57- asSet .contains ("S" );
58- }
59- duration = System .nanoTime () - startTime ;
60- System .out .println ("Set: " + duration / 10000 );
6158
62- startTime = System .nanoTime ();
59+ @ Benchmark
60+ public void searchArrayReuseSet () {
61+
62+ int count = 1000 ;
63+ String [] strings = seedArray (count );
64+ Set <String > asSet = new HashSet <>(Arrays .asList (strings ));
6365 for (int i = 0 ; i < count ; i ++) {
64- searchLoop ( strings , "T " );
66+ asSet . contains ( "S " );
6567 }
66- duration = System .nanoTime () - startTime ;
67- System .out .println ("Loop: " + duration / 10000 );
68-
6968 }
7069
7170
72- @ Test
71+ @ Benchmark
7372 public void searchArrayBinarySearch () {
7473
75- int count = 10000 ;
74+ int count = 1000 ;
7675 String [] strings = seedArray (count );
7776 Arrays .sort (strings );
7877
@@ -81,7 +80,7 @@ public void searchArrayBinarySearch() {
8180 Arrays .binarySearch (strings , "A" );
8281 }
8382 long duration = System .nanoTime () - startTime ;
84- System .out .println ("Binary search: " + duration / 10000 );
83+ // System.out.println("Binary search: " + duration / 10000);
8584
8685 }
8786
0 commit comments