Emil Kantis
11/20/2024, 12:06 PMschalkms
11/21/2024, 9:53 PMForEachOnRange
rule is still relevant. See here for more information regarding the official APIs.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/for-each.html
> But if you take a closer look at the signature of the forEach() function used here, you’ll notice that it’s not optimized for ranges but only for Iterable, so it requires the creation of an iterator.
Source: https://bladecoder.medium.com/exploring-kotlins-hidden-costs-part-3-3bf6e0dbf0a4schalkms
11/21/2024, 10:07 PMEmil Kantis
11/22/2024, 6:47 AMmain summary:
Benchmark (iterationsPerForEach) Mode Cnt Score Error Units
MyBenchmark.forLoop 10 thrpt 5 449993800,059 ± 26032787,135 ops/s
MyBenchmark.forLoop 100 thrpt 5 123058934,812 ± 803845,361 ops/s
MyBenchmark.rangedForEach 10 thrpt 5 212422928,653 ± 2200979,906 ops/s
MyBenchmark.rangedForEach 100 thrpt 5 20090642,098 ± 1329771,102 ops/s
Emil Kantis
11/22/2024, 7:06 AM