=================
== The Archive ==
=================

For Loop Comparison in Kotlin

|

Introduction

Benchmark Result

[Range] For Loop

1
2
3
4
5
6
@Benchmark
fun rangeForLoop(blackhole: Blackhole) {
    for (i in INT_RANGE) {
        blackhole.consume(i)
    }
}
 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
main: benchmark.ForLoopComparisonBenchmark.rangeForLoop

Warm-up 1: 1.406 ms/op
Warm-up 2: 1.393 ms/op
Warm-up 3: 1.393 ms/op
Warm-up 4: 1.393 ms/op
Warm-up 5: 1.393 ms/op
Warm-up 6: 1.393 ms/op
Warm-up 7: 1.392 ms/op
Warm-up 8: 1.393 ms/op
Warm-up 9: 1.393 ms/op
Warm-up 10: 1.393 ms/op
Iteration 1: 1.408 ms/op
Iteration 2: 1.388 ms/op
Iteration 3: 1.404 ms/op
Iteration 4: 1.387 ms/op
Iteration 5: 1.388 ms/op
Iteration 6: 1.388 ms/op
Iteration 7: 1.388 ms/op
Iteration 8: 1.388 ms/op
Iteration 9: 1.388 ms/op
Iteration 10: 1.387 ms/op
Iteration 11: 1.388 ms/op
Iteration 12: 1.388 ms/op
Iteration 13: 1.388 ms/op
Iteration 14: 1.388 ms/op
Iteration 15: 1.389 ms/op
Iteration 16: 1.392 ms/op
Iteration 17: 1.388 ms/op
Iteration 18: 1.418 ms/op
Iteration 19: 1.405 ms/op
Iteration 20: 1.394 ms/op

1.392 ±(99.9%) 0.008 ms/op [Average]
  (min, avg, max) = (1.387, 1.392, 1.418), stdev = 0.009
  CI (99.9%): [1.385, 1.400] (assumes normal distribution)

[Range] For Loop Another

1
2
3
4
5
6
@Benchmark
fun rangeForLoopAnother(blackhole: Blackhole) {
    for (i in 0 until NUMBER_OF_ITERATIONS) {
        blackhole.consume(i)
    }
}
 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
main: benchmark.ForLoopComparisonBenchmark.rangeForLoopAnother

Warm-up 1: 1.892 ms/op
Warm-up 2: 1.757 ms/op
Warm-up 3: 1.757 ms/op
Warm-up 4: 1.758 ms/op
Warm-up 5: 1.759 ms/op
Warm-up 6: 1.758 ms/op
Warm-up 7: 1.757 ms/op
Warm-up 8: 1.757 ms/op
Warm-up 9: 1.757 ms/op
Warm-up 10: 1.767 ms/op
Iteration 1: 1.750 ms/op
Iteration 2: 1.752 ms/op
Iteration 3: 1.749 ms/op
Iteration 4: 1.777 ms/op
Iteration 5: 1.750 ms/op
Iteration 6: 1.748 ms/op
Iteration 7: 1.749 ms/op
Iteration 8: 1.749 ms/op
Iteration 9: 1.749 ms/op
Iteration 10: 1.749 ms/op
Iteration 11: 1.749 ms/op
Iteration 12: 1.750 ms/op
Iteration 13: 1.750 ms/op
Iteration 14: 1.750 ms/op
Iteration 15: 1.750 ms/op
Iteration 16: 1.749 ms/op
Iteration 17: 1.749 ms/op
Iteration 18: 1.752 ms/op
Iteration 19: 1.755 ms/op
Iteration 20: 1.749 ms/op

1.751 ±(99.9%) 0.005 ms/op [Average]
  (min, avg, max) = (1.748, 1.751, 1.777), stdev = 0.006
  CI (99.9%): [1.746, 1.757] (assumes normal distribution)

[Range] forEach

1
2
3
4
@Benchmark
fun rangeForEach(blackhole: Blackhole) {
    INT_RANGE.forEach { blackhole.consume(it) }
}
 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
main: benchmark.ForLoopComparisonBenchmark.rangeForEach

Warm-up 1: 13.143 ms/op
Warm-up 2: 12.553 ms/op
Warm-up 3: 12.557 ms/op
Warm-up 4: 12.551 ms/op
Warm-up 5: 12.565 ms/op
Warm-up 6: 12.554 ms/op
Warm-up 7: 12.547 ms/op
Warm-up 8: 12.556 ms/op
Warm-up 9: 12.553 ms/op
Warm-up 10: 12.550 ms/op
Iteration 1: 12.615 ms/op
Iteration 2: 12.527 ms/op
Iteration 3: 12.538 ms/op
Iteration 4: 12.521 ms/op
Iteration 5: 12.580 ms/op
Iteration 6: 12.520 ms/op
Iteration 7: 12.540 ms/op
Iteration 8: 12.516 ms/op
Iteration 9: 12.516 ms/op
Iteration 10: 12.676 ms/op
Iteration 11: 12.589 ms/op
Iteration 12: 12.543 ms/op
Iteration 13: 12.561 ms/op
Iteration 14: 12.515 ms/op
Iteration 15: 12.602 ms/op
Iteration 16: 12.518 ms/op
Iteration 17: 12.513 ms/op
Iteration 18: 12.509 ms/op
Iteration 19: 12.611 ms/op
Iteration 20: 12.707 ms/op

12.561 ±(99.9%) 0.049 ms/op [Average]
  (min, avg, max) = (12.509, 12.561, 12.707), stdev = 0.057
  CI (99.9%): [12.511, 12.610] (assumes normal distribution)

[Range] forEachIndexed

1
2
3
4
@Benchmark
fun rangeForEachIndexed(blackhole: Blackhole) {
    INT_RANGE.forEachIndexed { _, index -> blackhole.consume(index) }
}
 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
main: benchmark.ForLoopComparisonBenchmark.rangeForEachIndexed

Warm-up 1: 14.257 ms/op
Warm-up 2: 13.777 ms/op
Warm-up 3: 13.775 ms/op
Warm-up 4: 13.772 ms/op
Warm-up 5: 13.773 ms/op
Warm-up 6: 13.778 ms/op
Warm-up 7: 13.770 ms/op
Warm-up 8: 13.776 ms/op
Warm-up 9: 13.769 ms/op
Warm-up 10: 13.772 ms/op
Iteration 1: 13.737 ms/op
Iteration 2: 13.786 ms/op
Iteration 3: 13.731 ms/op
Iteration 4: 13.762 ms/op
Iteration 5: 13.741 ms/op
Iteration 6: 13.884 ms/op
Iteration 7: 13.798 ms/op
Iteration 8: 13.742 ms/op
Iteration 9: 13.767 ms/op
Iteration 10: 13.751 ms/op
Iteration 11: 13.798 ms/op
Iteration 12: 13.733 ms/op
Iteration 13: 13.751 ms/op
Iteration 14: 13.732 ms/op
Iteration 15: 13.856 ms/op
Iteration 16: 13.848 ms/op
Iteration 17: 13.755 ms/op
Iteration 18: 13.757 ms/op
Iteration 19: 13.773 ms/op
Iteration 20: 13.782 ms/op

13.774 ±(99.9%) 0.038 ms/op [Average]
  (min, avg, max) = (13.731, 13.774, 13.884), stdev = 0.044
  CI (99.9%): [13.736, 13.812] (assumes normal distribution)

[Range] iterator

1
2
3
4
5
6
7
@Benchmark
fun rangeIterator(blackhole: Blackhole) {
    val iterator = INT_RANGE.iterator()
    while (iterator.hasNext()) {
        blackhole.consume(iterator.nextInt())
    }
}
 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
main: benchmark.ForLoopComparisonBenchmark.rangeIterator

Warm-up 1: 13.137 ms/op
Warm-up 2: 12.555 ms/op
Warm-up 3: 12.549 ms/op
Warm-up 4: 12.566 ms/op
Warm-up 5: 12.550 ms/op
Warm-up 6: 12.555 ms/op
Warm-up 7: 13.810 ms/op
Warm-up 8: 12.552 ms/op
Warm-up 9: 12.654 ms/op
Warm-up 10: 13.488 ms/op
Iteration 1: 12.524 ms/op
Iteration 2: 12.511 ms/op
Iteration 3: 12.511 ms/op
Iteration 4: 12.524 ms/op
Iteration 5: 12.559 ms/op
Iteration 6: 12.516 ms/op
Iteration 7: 12.520 ms/op
Iteration 8: 12.540 ms/op
Iteration 9: 12.517 ms/op
Iteration 10: 12.595 ms/op
Iteration 11: 12.560 ms/op
Iteration 12: 12.574 ms/op
Iteration 13: 12.543 ms/op
Iteration 14: 12.510 ms/op
Iteration 15: 12.604 ms/op
Iteration 16: 12.552 ms/op
Iteration 17: 12.513 ms/op
Iteration 18: 12.541 ms/op
Iteration 19: 12.543 ms/op
Iteration 20: 12.535 ms/op

12.540 ±(99.9%) 0.024 ms/op [Average]
  (min, avg, max) = (12.510, 12.540, 12.604), stdev = 0.028
  CI (99.9%): [12.516, 12.564] (assumes normal distribution)

[Collection] For Loop

1
2
3
4
5
6
@Benchmark
fun collectionForLoop(blackhole: Blackhole) {
    for (i in INT_LIST) {
        blackhole.consume(i)
    }
}
 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
main: benchmark.ForLoopComparisonBenchmark.collectionForLoop

Warm-up 1: 9.680 ms/op
Warm-up 2: 9.188 ms/op
Warm-up 3: 9.185 ms/op
Warm-up 4: 9.261 ms/op
Warm-up 5: 9.197 ms/op
Warm-up 6: 9.188 ms/op
Warm-up 7: 9.358 ms/op
Warm-up 8: 9.185 ms/op
Warm-up 9: 9.182 ms/op
Warm-up 10: 9.197 ms/op
Iteration 1: 9.300 ms/op
Iteration 2: 9.395 ms/op
Iteration 3: 9.170 ms/op
Iteration 4: 9.193 ms/op
Iteration 5: 9.207 ms/op
Iteration 6: 9.188 ms/op
Iteration 7: 9.181 ms/op
Iteration 8: 9.225 ms/op
Iteration 9: 9.218 ms/op
Iteration 10: 9.153 ms/op
Iteration 11: 9.190 ms/op
Iteration 12: 9.181 ms/op
Iteration 13: 9.327 ms/op
Iteration 14: 9.320 ms/op
Iteration 15: 9.292 ms/op
Iteration 16: 9.144 ms/op
Iteration 17: 9.154 ms/op
Iteration 18: 9.206 ms/op
Iteration 19: 9.209 ms/op
Iteration 20: 9.161 ms/op

9.221 ±(99.9%) 0.060 ms/op [Average]
  (min, avg, max) = (9.144, 9.221, 9.395), stdev = 0.069
  CI (99.9%): [9.161, 9.281] (assumes normal distribution)

[Collection] forEach

1
2
3
4
@Benchmark
fun collectionForEach(blackhole: Blackhole) {
    INT_LIST.forEach { blackhole.consume(it) }
}
 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
main: benchmark.ForLoopComparisonBenchmark.collectionForEach

Warm-up 1: 9.783 ms/op
Warm-up 2: 9.193 ms/op
Warm-up 3: 9.208 ms/op
Warm-up 4: 9.198 ms/op
Warm-up 5: 9.198 ms/op
Warm-up 6: 9.192 ms/op
Warm-up 7: 9.207 ms/op
Warm-up 8: 9.204 ms/op
Warm-up 9: 9.228 ms/op
Warm-up 10: 9.272 ms/op
Iteration 1: 9.322 ms/op
Iteration 2: 9.270 ms/op
Iteration 3: 9.175 ms/op
Iteration 4: 9.168 ms/op
Iteration 5: 9.226 ms/op
Iteration 6: 9.230 ms/op
Iteration 7: 9.323 ms/op
Iteration 8: 9.354 ms/op
Iteration 9: 9.176 ms/op
Iteration 10: 9.275 ms/op
Iteration 11: 9.209 ms/op
Iteration 12: 9.261 ms/op
Iteration 13: 9.171 ms/op
Iteration 14: 9.191 ms/op
Iteration 15: 9.219 ms/op
Iteration 16: 9.172 ms/op
Iteration 17: 9.159 ms/op
Iteration 18: 9.169 ms/op
Iteration 19: 9.175 ms/op
Iteration 20: 9.156 ms/op

9.220 ±(99.9%) 0.053 ms/op [Average]
  (min, avg, max) = (9.156, 9.220, 9.354), stdev = 0.061
  CI (99.9%): [9.167, 9.273] (assumes normal distribution)

[Collection] forEachIndexed

1
2
3
4
@Benchmark
fun collectionForEachIndexed(blackhole: Blackhole) {
    INT_LIST.forEachIndexed { _, index -> blackhole.consume(index) }
}
 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
main: benchmark.ForLoopComparisonBenchmark.collectionForEachIndexed

Warm-up 1: 10.870 ms/op
Warm-up 2: 9.228 ms/op
Warm-up 3: 9.285 ms/op
Warm-up 4: 9.212 ms/op
Warm-up 5: 9.226 ms/op
Warm-up 6: 9.219 ms/op
Warm-up 7: 9.218 ms/op
Warm-up 8: 9.399 ms/op
Warm-up 9: 9.225 ms/op
Warm-up 10: 9.226 ms/op
Iteration 1: 9.181 ms/op
Iteration 2: 9.228 ms/op
Iteration 3: 9.166 ms/op
Iteration 4: 9.182 ms/op
Iteration 5: 9.206 ms/op
Iteration 6: 9.192 ms/op
Iteration 7: 9.191 ms/op
Iteration 8: 9.392 ms/op
Iteration 9: 9.390 ms/op
Iteration 10: 9.175 ms/op
Iteration 11: 9.152 ms/op
Iteration 12: 9.298 ms/op
Iteration 13: 9.206 ms/op
Iteration 14: 9.195 ms/op
Iteration 15: 9.257 ms/op
Iteration 16: 9.172 ms/op
Iteration 17: 9.198 ms/op
Iteration 18: 9.221 ms/op
Iteration 19: 9.167 ms/op
Iteration 20: 9.222 ms/op

9.220 ±(99.9%) 0.059 ms/op [Average]
  (min, avg, max) = (9.152, 9.220, 9.392), stdev = 0.068
  CI (99.9%): [9.161, 9.278] (assumes normal distribution)

[Collection] iterator

1
2
3
4
5
6
7
@Benchmark
fun collectionIterator(blackhole: Blackhole) {
    val iterator = INT_LIST.iterator()
    while (iterator.hasNext()) {
        blackhole.consume(iterator.next())
    }
}
 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
main: benchmark.ForLoopComparisonBenchmark.collectionIterator

Warm-up 1: 9.772 ms/op
Warm-up 2: 9.253 ms/op
Warm-up 3: 9.252 ms/op
Warm-up 4: 9.252 ms/op
Warm-up 5: 9.310 ms/op
Warm-up 6: 9.522 ms/op
Warm-up 7: 9.257 ms/op
Warm-up 8: 9.263 ms/op
Warm-up 9: 9.279 ms/op
Warm-up 10: 9.253 ms/op
Iteration 1: 9.269 ms/op
Iteration 2: 9.218 ms/op
Iteration 3: 9.476 ms/op
Iteration 4: 9.577 ms/op
Iteration 5: 9.258 ms/op
Iteration 6: 9.271 ms/op
Iteration 7: 9.264 ms/op
Iteration 8: 9.239 ms/op
Iteration 9: 9.210 ms/op
Iteration 10: 9.308 ms/op
Iteration 11: 9.223 ms/op
Iteration 12: 9.217 ms/op
Iteration 13: 9.231 ms/op
Iteration 14: 9.234 ms/op
Iteration 15: 9.225 ms/op
Iteration 16: 9.328 ms/op
Iteration 17: 9.406 ms/op
Iteration 18: 9.361 ms/op
Iteration 19: 9.238 ms/op
Iteration 20: 9.383 ms/op

9.297 ±(99.9%) 0.086 ms/op [Average]
  (min, avg, max) = (9.210, 9.297, 9.577), stdev = 0.099
  CI (99.9%): [9.211, 9.382] (assumes normal distribution)

결과

RangeCollection
For Loop1.392 ±(99.9%) 0.008 ms/op [Average]9.221 ±(99.9%) 0.060 ms/op [Average]
forEach12.561 ±(99.9%) 0.049 ms/op [Average]9.220 ±(99.9%) 0.053 ms/op [Average]
forEachIndexed13.774 ±(99.9%) 0.038 ms/op [Average]9.220 ±(99.9%) 0.059 ms/op [Average]
Iterator12.540 ±(99.9%) 0.024 ms/op [Average]9.297 ±(99.9%) 0.086 ms/op [Average]

Why?

  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
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
@Benchmark
public void rangeForLoop(@NotNull Blackhole blackhole) {
  Intrinsics.checkNotNullParameter(blackhole, "blackhole");
  IntRange var2 = INT_RANGE;
  int i = var2.getFirst();
  int var4 = var2.getLast();
  if (i <= var4) {
     while(true) {
        blackhole.consume(i);
        if (i == var4) {
           break;
        }

        ++i;
     }
  }

}

@Benchmark
public void rangeForLoopAnother(@NotNull Blackhole blackhole) {
  Intrinsics.checkNotNullParameter(blackhole, "blackhole");

  for(int i = 0; i < 15000000; ++i) {
     blackhole.consume(i);
  }

}

@Benchmark
public void rangeForEach(@NotNull Blackhole blackhole) {
  Intrinsics.checkNotNullParameter(blackhole, "blackhole");
  Iterable $this$forEach$iv = (Iterable)INT_RANGE;
  int $i$f$forEach = 0;
  Iterator var4 = $this$forEach$iv.iterator();

  while(var4.hasNext()) {
     int element$iv = ((IntIterator)var4).nextInt();
     int var7 = 0;
     blackhole.consume(element$iv);
  }

}

@Benchmark
public void rangeForEachIndexed(@NotNull Blackhole blackhole) {
  Intrinsics.checkNotNullParameter(blackhole, "blackhole");
  Iterable $this$forEachIndexed$iv = (Iterable)INT_RANGE;
  int $i$f$forEachIndexed = 0;
  int index$iv = 0;
  Iterator var5 = $this$forEachIndexed$iv.iterator();

  while(var5.hasNext()) {
     int item$iv = ((IntIterator)var5).nextInt();
     int var7 = index$iv++;
     if (var7 < 0) {
        CollectionsKt.throwIndexOverflow();
     }

     int var10 = 0;
     blackhole.consume(item$iv);
  }

}

@Benchmark
public void rangeIterator(@NotNull Blackhole blackhole) {
  Intrinsics.checkNotNullParameter(blackhole, "blackhole");
  IntIterator iterator = INT_RANGE.iterator();

  while(iterator.hasNext()) {
     blackhole.consume(iterator.nextInt());
  }

}

@Benchmark
public void collectionForLoop(@NotNull Blackhole blackhole) {
  Intrinsics.checkNotNullParameter(blackhole, "blackhole");
  Iterator var2 = INT_LIST.iterator();

  while(var2.hasNext()) {
     int i = ((Number)var2.next()).intValue();
     blackhole.consume(i);
  }

}

@Benchmark
public void collectionForEach(@NotNull Blackhole blackhole) {
  Intrinsics.checkNotNullParameter(blackhole, "blackhole");
  Iterable $this$forEach$iv = (Iterable)INT_LIST;
  int $i$f$forEach = 0;

  for(Object element$iv : $this$forEach$iv) {
     int it = ((Number)element$iv).intValue();
     int var7 = 0;
     blackhole.consume(it);
  }

}

@Benchmark
public void collectionForEachIndexed(@NotNull Blackhole blackhole) {
  Intrinsics.checkNotNullParameter(blackhole, "blackhole");
  Iterable $this$forEachIndexed$iv = (Iterable)INT_LIST;
  int $i$f$forEachIndexed = 0;
  int index$iv = 0;

  for(Object item$iv : $this$forEachIndexed$iv) {
     int var7 = index$iv++;
     if (var7 < 0) {
        CollectionsKt.throwIndexOverflow();
     }

     int index = ((Number)item$iv).intValue();
     int var10 = 0;
     blackhole.consume(index);
  }

}

@Benchmark
public void collectionIterator(@NotNull Blackhole blackhole) {
  Intrinsics.checkNotNullParameter(blackhole, "blackhole");
  Iterator iterator = INT_LIST.iterator();

  while(iterator.hasNext()) {
     blackhole.consume(((Number)iterator.next()).intValue());
  }

}

결론

Source Code

 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
64
65
66
67
68
69
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(BenchmarkTimeUnit.MILLISECONDS)
@Warmup(iterations = 10, time = 500, timeUnit = BenchmarkTimeUnit.MILLISECONDS)
@Measurement(iterations = 20, time = 1, timeUnit = BenchmarkTimeUnit.MILLISECONDS)
@State(Scope.Benchmark)
class ForLoopComparisonBenchmark {
    companion object {
        private const val NUMBER_OF_ITERATIONS = 15_000_000
        private val INT_RANGE = 0 until NUMBER_OF_ITERATIONS
        private val INT_LIST = INT_RANGE.toList()
    }

    @Benchmark
    fun rangeForLoop(blackhole: Blackhole) {
        for (i in INT_RANGE) {
            blackhole.consume(i)
        }
    }

    @Benchmark
    fun rangeForLoopAnother(blackhole: Blackhole) {
        for (i in 0 until NUMBER_OF_ITERATIONS) {
            blackhole.consume(i)
        }
    }

    @Benchmark
    fun rangeForEach(blackhole: Blackhole) {
        INT_RANGE.forEach { blackhole.consume(it) }
    }

    @Benchmark
    fun rangeForEachIndexed(blackhole: Blackhole) {
        INT_RANGE.forEachIndexed { _, index -> blackhole.consume(index) }
    }

    @Benchmark
    fun rangeIterator(blackhole: Blackhole) {
        val iterator = INT_RANGE.iterator()
        while (iterator.hasNext()) {
            blackhole.consume(iterator.nextInt())
        }
    }

    @Benchmark
    fun collectionForLoop(blackhole: Blackhole) {
        for (i in INT_LIST) {
            blackhole.consume(i)
        }
    }

    @Benchmark
    fun collectionForEach(blackhole: Blackhole) {
        INT_LIST.forEach { blackhole.consume(it) }
    }

    @Benchmark
    fun collectionForEachIndexed(blackhole: Blackhole) {
        INT_LIST.forEachIndexed { _, index -> blackhole.consume(index) }
    }

    @Benchmark
    fun collectionIterator(blackhole: Blackhole) {
        val iterator = INT_LIST.iterator()
        while (iterator.hasNext()) {
            blackhole.consume(iterator.next())
        }
    }
}

References

Categories:

Tags: