1 /*
 2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
 3  */
 4 package org.openjdk.bench.valhalla.sandbox.corelibs.corelibs;
 5 
 6 import java.util.List;
 7 import java.util.Iterator;
 8 import java.util.concurrent.TimeUnit;
 9 
10 import org.openjdk.jmh.annotations.*;
11 import org.openjdk.jmh.infra.Blackhole;
12 
13 @Fork(1)
14 @Warmup(iterations = 3, time = 1)
15 @Measurement(iterations = 5, time = 3)
16 @OutputTimeUnit(TimeUnit.MILLISECONDS)
17 @BenchmarkMode(Mode.AverageTime)
18 @State(Scope.Thread)
19 public class XArrayListCursorTest {
20     @Param({"100000"})
21     public static int size;
22 
23     private static final String constantString = "abc";
24 
25     private static XArrayList<String> list;
26 
27     @Setup
28     public void setup() {
29         list = new XArrayList<>();
30         for (int i = 0; i < size; i++) {
31             list.add(constantString);
32         }
33     }
34 
35     @Benchmark
36     public void getViaCursorWhileLoop(Blackhole blackhole) {
37         InlineCursor<String> cur = list.cursor();
38         while (cur.exists()) {
39             blackhole.consume(cur.get());
40             cur = cur.advance();
41         }
42     }
43 
44     @Benchmark
45     public void getViaCursorForLoop(Blackhole blackhole) {
46         for (InlineCursor<String> cur = list.cursor();
47              cur.exists();
48              cur = cur.advance()) {
49             blackhole.consume(cur.get());
50         }
51     }
52 
53     @Benchmark
54     public void getViaIterator(Blackhole blackhole) {
55         Iterator<String> it = list.iterator();
56         while (it.hasNext()) {
57             blackhole.consume(it.next());
58         }
59     }
60 
61     @Benchmark
62     public void getViaIteratorCurs(Blackhole blackhole) {
63         Iterator<String> it = list.iteratorCurs();
64         while (it.hasNext()) {
65             blackhole.consume(it.next());
66         }
67     }
68 
69     @Benchmark
70     public void getViaArray(Blackhole blackhole) {
71         for (int i = 0; i < list.size(); i++) {
72             blackhole.consume(list.get(i));
73         }
74     }
75 
76 }