Skip to content

Instantly share code, notes, and snippets.

@tkrs
Last active March 22, 2018 01:32
Show Gist options
  • Select an option

  • Save tkrs/eca46009353046fdc1444e17cf8eb557 to your computer and use it in GitHub Desktop.

Select an option

Save tkrs/eca46009353046fdc1444e17cf8eb557 to your computer and use it in GitHub Desktop.
Array[Byte] Concatenate Benchmark
import java.nio.ByteBuffer
import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._
@State(Scope.Thread)
@BenchmarkMode(Array(Mode.Throughput))
@Warmup(iterations = 10, time = 1)
@Measurement(iterations = 10, time = 1)
@OutputTimeUnit(TimeUnit.SECONDS)
@Fork(2)
class ByteArrayBench {
val source1: Array[Byte] = (1 to 100).map(_.toByte).toArray
val source2: Array[Byte] = (1 to 100).map(_.toByte).toArray
@Benchmark
def arrayBuilder: Array[Byte] = {
val b = Array.newBuilder[Byte]
b.sizeHint(source1.length + source2.length)
b ++= source1
b ++= source2
b.result()
}
@Benchmark
def arrayCopy: Array[Byte] = {
val dest = Array.ofDim[Byte](source1.length + source2.length)
java.lang.System.arraycopy(source1, 0, dest, 0, source1.length)
java.lang.System.arraycopy(source2, 0, dest, source1.length, source1.length)
dest
}
@Benchmark
def arrayAppend: Array[Byte] = {
source1 ++ source2
}
@Benchmark
def byteBuffer: ByteBuffer = {
val s1 = ByteBuffer.wrap(source1)
val s2 = ByteBuffer.wrap(source2)
val dest = ByteBuffer.allocate(s1.remaining() + s2.remaining())
dest.put(s1)
dest.put(s2)
dest.flip()
dest
}
}
[info] Benchmark Mode Cnt Score Error Units
[info] ArrayBench.arrayAppend thrpt 20 9708780.666 ± 1344125.597 ops/s
[info] ArrayBench.arrayAppend:·gc.alloc.rate thrpt 20 1972.252 ± 273.628 MB/sec
[info] ArrayBench.arrayAppend:·gc.alloc.rate.norm thrpt 20 320.000 ± 0.001 B/op
[info] ArrayBench.arrayAppend:·gc.churn.PS_Eden_Space thrpt 20 1985.533 ± 279.831 MB/sec
[info] ArrayBench.arrayAppend:·gc.churn.PS_Eden_Space.norm thrpt 20 322.233 ± 9.895 B/op
[info] ArrayBench.arrayAppend:·gc.churn.PS_Survivor_Space thrpt 20 0.083 ± 0.033 MB/sec
[info] ArrayBench.arrayAppend:·gc.churn.PS_Survivor_Space.norm thrpt 20 0.013 ± 0.005 B/op
[info] ArrayBench.arrayAppend:·gc.count thrpt 20 278.000 counts
[info] ArrayBench.arrayAppend:·gc.time thrpt 20 173.000 ms
[info] ArrayBench.arrayBuilder thrpt 20 795572.672 ± 21835.642 ops/s
[info] ArrayBench.arrayBuilder:·gc.alloc.rate thrpt 20 121.222 ± 3.351 MB/sec
[info] ArrayBench.arrayBuilder:·gc.alloc.rate.norm thrpt 20 240.002 ± 0.004 B/op
[info] ArrayBench.arrayBuilder:·gc.churn.PS_Eden_Space thrpt 20 122.448 ± 6.772 MB/sec
[info] ArrayBench.arrayBuilder:·gc.churn.PS_Eden_Space.norm thrpt 20 242.474 ± 12.114 B/op
[info] ArrayBench.arrayBuilder:·gc.churn.PS_Survivor_Space thrpt 20 0.052 ± 0.022 MB/sec
[info] ArrayBench.arrayBuilder:·gc.churn.PS_Survivor_Space.norm thrpt 20 0.103 ± 0.046 B/op
[info] ArrayBench.arrayBuilder:·gc.count thrpt 20 116.000 counts
[info] ArrayBench.arrayBuilder:·gc.time thrpt 20 62.000 ms
[info] ArrayBench.arrayCopy thrpt 20 23068792.759 ± 926859.198 ops/s
[info] ArrayBench.arrayCopy:·gc.alloc.rate thrpt 20 3163.978 ± 128.644 MB/sec
[info] ArrayBench.arrayCopy:·gc.alloc.rate.norm thrpt 20 216.000 ± 0.001 B/op
[info] ArrayBench.arrayCopy:·gc.churn.PS_Eden_Space thrpt 20 3165.939 ± 166.427 MB/sec
[info] ArrayBench.arrayCopy:·gc.churn.PS_Eden_Space.norm thrpt 20 216.100 ± 6.547 B/op
[info] ArrayBench.arrayCopy:·gc.churn.PS_Survivor_Space thrpt 20 0.058 ± 0.022 MB/sec
[info] ArrayBench.arrayCopy:·gc.churn.PS_Survivor_Space.norm thrpt 20 0.004 ± 0.002 B/op
[info] ArrayBench.arrayCopy:·gc.count thrpt 20 326.000 counts
[info] ArrayBench.arrayCopy:·gc.time thrpt 20 183.000 ms
[info] ArrayBench.byteBuffer thrpt 20 20291680.952 ± 536843.080 ops/s
[info] ArrayBench.byteBuffer:·gc.alloc.rate thrpt 20 3400.674 ± 90.675 MB/sec
[info] ArrayBench.byteBuffer:·gc.alloc.rate.norm thrpt 20 264.000 ± 0.001 B/op
[info] ArrayBench.byteBuffer:·gc.churn.PS_Eden_Space thrpt 20 3398.325 ± 151.210 MB/sec
[info] ArrayBench.byteBuffer:·gc.churn.PS_Eden_Space.norm thrpt 20 263.732 ± 7.316 B/op
[info] ArrayBench.byteBuffer:·gc.churn.PS_Survivor_Space thrpt 20 0.112 ± 0.041 MB/sec
[info] ArrayBench.byteBuffer:·gc.churn.PS_Survivor_Space.norm thrpt 20 0.009 ± 0.003 B/op
[info] ArrayBench.byteBuffer:·gc.count thrpt 20 313.000 counts
[info] ArrayBench.byteBuffer:·gc.time thrpt 20 173.000 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment