Skip to content

Instantly share code, notes, and snippets.

@ktoso
Created July 28, 2015 18:54
Show Gist options
  • Save ktoso/6f1ed3dc192b24d93f97 to your computer and use it in GitHub Desktop.
Save ktoso/6f1ed3dc192b24d93f97 to your computer and use it in GitHub Desktop.
package test
import org.openjdk.jmh.annotations._
import scala.collection.immutable.Stack
import scala.collection.mutable
/*
Benchmark Mode Cnt Score Error Units
TestBenchmark.stack_1000 thrpt 20 7.446 ± 0.338 ops/ms
TestBenchmark.vector_1000 thrpt 20 19401.495 ± 754.851 ops/ms
TestBenchmark.vector_builder_1000 thrpt 20 142296.432 ± 40669.008 ops/ms
*/
@State(Scope.Benchmark)
@BenchmarkMode(Array(Mode.Throughput))
class TestBenchmark {
import scala.collection.immutable
var b: mutable.Builder[Any, Vector[Any]] = _
var v: immutable.Vector[Any] = _
var s: immutable.Stack[Any] = _
@Setup(Level.Iteration)
def init = {
b = Vector.newBuilder[Any]
b.sizeHint(1000)
v = Vector.empty[Any]
s = Stack.empty[Any]
}
@Benchmark
@OperationsPerInvocation(1000)
def vector_1000 = {
1 to 1000 foreach { _ => v :+= "" }
v
}
@Benchmark
@OperationsPerInvocation(1000)
def stack_1000 = {
1 to 1000 foreach { _ => s :+= "" }
s
}
@Benchmark
@OperationsPerInvocation(1000)
def vector_builder_1000 = {
1 to 1000 foreach { _ => b += "" }
b.result()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment