Skip to content

Instantly share code, notes, and snippets.

@SergejIsbrecht
Created April 13, 2020 18:32
Show Gist options
  • Save SergejIsbrecht/5756f998c7bac6f8bce948fbcf4e4ed9 to your computer and use it in GitHub Desktop.
Save SergejIsbrecht/5756f998c7bac6f8bce948fbcf4e4ed9 to your computer and use it in GitHub Desktop.
Benchmark Range
package com.example.benchmark
import androidx.benchmark.junit4.BenchmarkRule
import androidx.benchmark.junit4.measureRepeated
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import io.reactivex.rxjava3.core.Flowable
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@LargeTest
@RunWith(AndroidJUnit4::class)
class Iterate {
@get:Rule
val benchmarkRule = BenchmarkRule()
@Test
fun kotlin_range_seq_last() {
benchmarkRule.measureRepeated {
(1..1_000_000)
.asSequence()
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.last()
}
}
@Test
fun kotlin_range_default_last() {
benchmarkRule.measureRepeated {
(1..1_000_000)
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.last()
}
}
@Test
fun kotlin_range_rxjava_last() {
benchmarkRule.measureRepeated {
Flowable.range(0, 1_000_000)
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.blockingLast()
}
}
@Test
fun kotlin_range_seq_first() {
benchmarkRule.measureRepeated {
(1..1_000_000)
.asSequence()
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.first()
}
}
@Test
fun kotlin_range_default_first() {
benchmarkRule.measureRepeated {
(1..1_000_000)
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.first()
}
}
@Test
fun kotlin_range_rxjava_first() {
benchmarkRule.measureRepeated {
Flowable.range(0, 1_000_000)
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.blockingFirst()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment