Skip to content

Instantly share code, notes, and snippets.

@squito
Last active December 18, 2015 17:39
Show Gist options
  • Save squito/5820134 to your computer and use it in GitHub Desktop.
Save squito/5820134 to your computer and use it in GitHub Desktop.
trait ArrayLike[@specialized T] {
def apply(idx: Int) : T
def update(idx: Int, value: T)
def length: Int
def size: Int = length
}
def sumArrayLike(arr: ArrayLike[Float]): Float = {
var idx = 0
var sum = 0f
while(idx < arr.length) {
sum += arr(idx)
idx += 1
}
sum
}
val n = 1e7.toInt
val (_, wrapped, buf, arrBuf) = initArrays(n) // now this returns classes that implement ArrayLike
println("result = " + th.pbench(sumArrayLike(wrapped), title="wrapped array"))
println("result = " + th.pbench(sumArrayLike(arrBuf), title="float array wrapped in FloatBuffer"))
println("result = " + th.pbench(sumArrayLike(buf), title="byte array wrapped in FloatBuffer"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment