Skip to content

Instantly share code, notes, and snippets.

@tomaszpolanski
Last active August 21, 2017 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomaszpolanski/6e5b79af2f1c3d1146bd042f3e81f2e7 to your computer and use it in GitHub Desktop.
Save tomaszpolanski/6e5b79af2f1c3d1146bd042f3e81f2e7 to your computer and use it in GitHub Desktop.
Declarative benchmark
package com.tomaszpolanski.androidsandbox.main
import android.util.Log
import io.reactivex.Observable
import ix.Ix
const val numberOfItems = 1_000_000
fun print(name: String, durationInMicroseconds: Long, speedPercentage: Int) =
Log.e("Benchmark", "$name, duration: $durationInMicroseconds μs, compared to slowest: $speedPercentage%")
class BenchmarkGroup(val group: String, task: BenchmarkGroup.() -> Unit) {
val benchmarkList: HashMap<String, Long> = HashMap()
init {
task()
val maxDuration = benchmarkList.values.max()
maxDuration?.let {
benchmarkList.entries
.forEach { (name, duration) ->
print("$group $name", duration, ((it.toFloat() / duration) * 100f).toInt())
}
}
}
fun benchmark(name: String, task: () -> Unit) {
val list = ArrayList<Long>()
for (i in 1..100) {
val before = System.nanoTime()
task()
val after = System.nanoTime()
val speed = (after - before) / 1_000
list.add(speed)
}
list.sort()
benchmarkList[name] = list[list.size / 2]
}
}
fun main() {
BenchmarkGroup("list") {
benchmark("rxjava") {
Observable.range(0, numberOfItems)
.map { it + 1 }
.blockingLast()
}
benchmark("ixjava") {
Ix.range(0, numberOfItems)
.map { it + 1 }
.last()
}
benchmark("kotlin") {
(1..numberOfItems)
.map { it + 1 }
.last()
}
benchmark("seq") {
(1..numberOfItems)
.asSequence()
.map { it + 1 }
.last()
}
}
BenchmarkGroup("multimapping") {
benchmark("rxjava") {
Observable.range(0, numberOfItems)
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.blockingLast()
}
benchmark("ixjava") {
Ix.range(0, numberOfItems)
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.last()
}
benchmark("kotlin") {
(1..numberOfItems)
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.last()
}
benchmark("seq") {
(1..numberOfItems)
.asSequence()
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.map { it + 1 }
.last()
}
}
BenchmarkGroup("first") {
benchmark("rxjava") {
Observable.range(0, numberOfItems)
.map { it + 1 }
.blockingFirst()
}
benchmark("ixjava") {
Ix.range(0, numberOfItems)
.map { it + 1 }
.first()
}
benchmark("kotlin") {
(1..numberOfItems)
.map { it + 1 }
.first()
}
benchmark("seq") {
(1..numberOfItems)
.asSequence()
.map { it + 1 }
.first()
}
}
}
Collection<Integer> destination = new ArrayList<>(list.size());
Iterable<Integer> iterable = list;
Iterator<Integer> iterator = iterable.iterator();
while (iterator.hasNext()) {
int it = iterator.next();
destination.add(it + 1);
}
iterable = destination;
destination = new ArrayList<>();
iterator = iterable.iterator();
while (iterator.hasNext()) {
int it = iterator.next();
if (it % 2 == 0) {
destination.add(it);
}
}
iterable = destination;
int count = 0;
iterator = iterable.iterator();
while (iterator.hasNext()) {
int it = iterator.next();
if (it < 10) {
++count;
}
}
return count;
import android.util.Log
const val numberOfItems = 1000
fun formatTime(name: String, durationInMicroseconds: Long, speedPercentage: Int) =
"$name, duration: $durationInMicroseconds μs, is faster by $speedPercentage%"
fun formatSlowestTime(name: String, durationInMicroseconds: Long) =
"$name, duration: $durationInMicroseconds μs, is the slowest"
class BenchmarkGroup(val group: String, task: BenchmarkGroup.() -> Unit) {
val benchmarkList: HashMap<String, Long> = HashMap()
init {
task()
val maxDuration = benchmarkList.values.max()
maxDuration?.let {
benchmarkList.entries
.sortedByDescending { (_, duration) -> duration }
.mapIndexed { index, (name, duration) ->
if (index == 0)
formatSlowestTime("$group $name", duration)
else
formatTime("$group $name", duration,
(100 - (duration * 100) / it).toInt())
}
.forEach { Log.e("Benchmark", it) }
}
}
fun benchmark(name: String, task: () -> Unit) {
val list = ArrayList<Long>()
for (i in 1..500) {
val before = System.nanoTime()
task()
val after = System.nanoTime()
val speed = (after - before) / 1_000
list.add(speed)
}
list.sort()
benchmarkList[name] = list[list.size / 2]
}
}
fun main() {
val list = (1..numberOfItems).toList()
BenchmarkGroup("filter") {
benchmark("kotlin") {
list
.filter { it % 100 == 0 }
.count { true }
}
benchmark("seq") {
list
.asSequence()
.filter { it % 100 == 0 }
.count { true }
}
}
BenchmarkGroup("map") {
benchmark("kotlin") {
list
.map { it + 1 }
.count { true }
}
benchmark("seq") {
list
.asSequence()
.map { it + 1 }
.count { true }
}
}
BenchmarkGroup("multiple") {
benchmark("kotlin") {
list
.map { it + 1 }
.filter { it % 2 == 0 }
.count { it < 10 }
}
benchmark("seq") {
list
.asSequence()
.map { it + 1 }
.filter { it % 2 == 0 }
.count { it < 10 }
}
}
BenchmarkGroup("first") {
benchmark("kotlin") {
list
.map { it + 1 }
.first { it % 100 == 0 }
}
benchmark("seq") {
list
.asSequence()
.map { it + 1 }
.first { it % 100 == 0 }
}
}
BenchmarkGroup("elementAt") {
benchmark("kotlin") {
list
.elementAt(500)
}
benchmark("seq") {
list
.asSequence()
.elementAt(500)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment