-
-
Save ForteScarlet/6969d1c39a929c99345388346f8f6f99 to your computer and use it in GitHub Desktop.
Sequence vs List benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import kotlinx.coroutines.flow.asFlow | |
| import kotlinx.coroutines.flow.filter | |
| import kotlinx.coroutines.flow.map | |
| import kotlinx.coroutines.flow.toList | |
| import kotlinx.coroutines.runBlocking | |
| import org.openjdk.jmh.annotations.Benchmark | |
| import org.openjdk.jmh.annotations.Fork | |
| import org.openjdk.jmh.annotations.Measurement | |
| import org.openjdk.jmh.annotations.Scope | |
| import org.openjdk.jmh.annotations.State | |
| import org.openjdk.jmh.annotations.Warmup | |
| import java.util.concurrent.TimeUnit | |
| @State(Scope.Benchmark) | |
| @Fork(1) | |
| @Warmup(iterations = 2) | |
| @Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS) | |
| class SequenceBenchmark { | |
| @Benchmark | |
| fun list() { | |
| DataRepository.getItemsList() | |
| } | |
| @Benchmark | |
| fun sequence() { | |
| DataRepository.getItemsListUsingSequence() | |
| } | |
| @Benchmark | |
| fun flow() { | |
| DataRepository.getItemsListUsingFlow() | |
| } | |
| } | |
| private const val ITEM_SIZE = 100 | |
| object Db { | |
| fun getItems(): List<DbModel> { | |
| return (0 until ITEM_SIZE).map { index -> | |
| DbModel( | |
| id = index, | |
| rank = ITEM_SIZE - index, // reverse | |
| isEnabled = index.mod(2) == 0, | |
| ) | |
| } | |
| } | |
| } | |
| object DataRepository { | |
| fun getItemsList(): List<UiModel> { | |
| return Db.getItems() | |
| .filter { it.isEnabled } | |
| .map { UiModel(it.id) } | |
| } | |
| fun getItemsListUsingSequence(): List<UiModel> { | |
| return Db.getItems() | |
| .asSequence() | |
| .filter { it.isEnabled } | |
| .map { UiModel(it.id) } | |
| .toList() | |
| } | |
| fun getItemsListUsingFlow(): List<UiModel> = runBlocking { | |
| Db.getItems() | |
| .asFlow() | |
| .filter { it.isEnabled } | |
| .map { UiModel(it.id) } | |
| .toList() | |
| } | |
| } | |
| data class DbModel(val id: Int, val rank: Int, val isEnabled: Boolean) | |
| data class UiModel(val id: Int) |
Author
Author
1. big_operation (size=100)
xychart-beta
title "Throughput big_operation (size=100)"
x-axis ["Flow", "List", "Sequence"]
y-axis "ops/s" 0 --> 350000
bar [158616, 309514, 297240]
2. big_operation (size=100000)
xychart-beta
title "Throughput big_operation (size=100000)"
x-axis ["Flow", "List", "Sequence"]
y-axis "ops/s" 0 --> 350
bar [198, 249, 304]
3. small_operation (size=100)
xychart-beta
title "Throughput small_operation (size=100)"
x-axis ["Flow", "List", "Sequence"]
y-axis "ops/s" 0 --> 2500000
bar [1343646, 1905597, 2333803]
4. small_operation (size=100000)
xychart-beta
title "Throughput small_operation (size=100000)"
x-axis ["Flow", "List", "Sequence"]
y-axis "ops/s" 0 --> 2500
bar [2231, 1876, 2413]
汇总表格 :
| Group | Size | Flow (ops/s) | List (ops/s) | Sequence (ops/s) |
|---|---|---|---|---|
big_operation |
100 | 158,616 | 309,514 | 297,240 |
big_operation |
100000 | 198 | 249 | 304 |
small_operation |
100 | 1,343,646 | 1,905,597 | 2,333,803 |
small_operation |
100000 | 2,231 | 1,876 | 2,413 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on the original benchmark I got the following results (size: 100):
I noticed that the original benchmark included the process of building the list in the test, but it should have nothing to do with the operation we want to test.
So I took the initialisation of the list out of the benchmark and parameterised the
size.And added a couple of tests with more operations.
The result:
Based on the benchmarking results above, it appears to be getting a different result than the original blog.
In the case of a small number of intermediate operations and a small number of elements, a
Listmay be slightly better than aSequence, but in other cases,Sequencealmost always prevails between the two.