Skip to content

Commit

Permalink
Added jmh benchmark for FLoatArrayBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
alexklibisz committed Mar 23, 2024
1 parent e2b4da0 commit 3842f08
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public void append(float f) {
}

public float[] toArray() {
nextInitialCapacity = Math.min(maxInitialCapacity, Math.max(minInitialCapacity, index));
if (nextInitialCapacity != index) {
nextInitialCapacity = Math.min(maxInitialCapacity, Math.max(minInitialCapacity, index));
}
if (this.array.length == index) {
return this.array;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.klibisz.elastiknn.jmhbenchmarks

import com.klibisz.elastiknn.api.FloatArrayBuffer
import org.openjdk.jmh.annotations._

import scala.collection.mutable.ArrayBuffer
import scala.util.Random

@State(Scope.Benchmark)
class FloatArrayBufferBenchmarksState {
implicit private val rng: Random = new Random(0)
val lst768 = (0 until 768).map(_ => rng.nextFloat()).toList
}

class FloatArrayBufferBenchmarks {

@Benchmark
@BenchmarkMode(Array(Mode.Throughput))
@Fork(value = 1)
@Warmup(time = 5, iterations = 1)
@Measurement(time = 5, iterations = 1)
def scalaAppendFixedInitialSize(state: FloatArrayBufferBenchmarksState): Int = {
val buf = new ArrayBuffer[Float]()
state.lst768.foreach(buf.append)
buf.toArray.length
}

@Benchmark
@BenchmarkMode(Array(Mode.Throughput))
@Fork(value = 1)
@Warmup(time = 5, iterations = 1)
@Measurement(time = 5, iterations = 1)
def scalaAppendKnownInitialSize(state: FloatArrayBufferBenchmarksState): Int = {
val buf = new ArrayBuffer[Float](768)
state.lst768.foreach(buf.append)
buf.toArray.length
}

@Benchmark
@BenchmarkMode(Array(Mode.Throughput))
@Fork(value = 1)
@Warmup(time = 5, iterations = 1)
@Measurement(time = 5, iterations = 1)
def customAppend(state: FloatArrayBufferBenchmarksState): Int = {
val buf = new FloatArrayBuffer()
state.lst768.foreach(buf.append)
buf.toArray.length
}
}

0 comments on commit 3842f08

Please sign in to comment.