Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package geotrellis.benchmark
- import geotrellis.vector._
- import geotrellis.raster._
- import geotrellis.raster.op.local._
- import com.google.caliper.Param
- import scala.util.Random
- import scala.annotation.tailrec
- import scala.reflect._
- trait IntFunc {
- def apply(x: Int, y: Int, z: Int): Int
- }
- trait IntFunc2[T] {
- def apply(x: Int, y: Int, z: Int): T
- }
- class ArrayWrapper(arr: Array[Int]) {
- val size = arr.size
- def function1MapToInt(f: Int => Int): Array[Int] = {
- val result = arr.clone
- var i = 0
- while(i < size) {
- val x = arr(i)
- result(i) = f(x)
- i += 1
- }
- result
- }
- def function1MapToT[T: ClassTag](f: Int => T): Array[T] = {
- val result = Array.ofDim[T](size)
- var i = 0
- while(i < size) {
- val x = arr(i)
- result(i) = f(x)
- i += 1
- }
- result
- }
- def function3MapToInt(f: (Int, Int, Int) => Int): Array[Int] = {
- val result = arr.clone
- var i = 0
- while(i < size) {
- val x = arr(i)
- result(i) = f(x, x, x)
- i += 1
- }
- result
- }
- def function3MapToT[T: ClassTag](f: (Int, Int, Int) => T): Array[T] = {
- val result = Array.ofDim[T](size)
- var i = 0
- while(i < size) {
- val x = arr(i)
- result(i) = f(x, x, x)
- i += 1
- }
- result
- }
- def traitMapToInt(f: IntFunc): Array[Int] = {
- val result = arr.clone
- var i = 0
- while(i < size) {
- val x = arr(i)
- result(i) = f(x, x, x)
- i += 1
- }
- result
- }
- def traitMapToT[T: ClassTag](f: IntFunc2[T]): Array[T] = {
- val result = Array.ofDim[T](size)
- var i = 0
- while(i < size) {
- val x = arr(i)
- result(i) = f(x, x, x)
- i += 1
- }
- result
- }
- }
- object BoxingBenchmark extends BenchmarkRunner(classOf[BoxingBenchmark])
- class BoxingBenchmark extends OperationBenchmark {
- @Param(Array("2048"))
- var size: Int = 0
- var ints: Array[Int] = null
- override def setUp() {
- val len = size * size
- ints = init(len)(Random.nextInt)
- }
- def timeFunction1MapToInt(reps: Int) = run(reps)(function1MapToInt)
- def function1MapToInt = {
- val goal = ints.clone
- val w = new ArrayWrapper(goal)
- val x = w.function1MapToInt{ z => z + 1 }
- x
- }
- def timeFunction1MapToT(reps: Int) = run(reps)(function1MapToT)
- def function1MapToT = {
- val goal = ints.clone
- val w = new ArrayWrapper(goal)
- val x = w.function1MapToT[Option[Int]] { z => Some(z) }
- x
- }
- def timeFunction3MapToInt(reps: Int) = run(reps)(function3MapToInt)
- def function3MapToInt = {
- val goal = ints.clone
- val w = new ArrayWrapper(goal)
- val x = w.function3MapToInt { (x, y, z) => z + 1 }
- x
- }
- def timeFunction3MapToT(reps: Int) = run(reps)(function3MapToT)
- def function3MapToT = {
- val goal = ints.clone
- val w = new ArrayWrapper(goal)
- val x = w.function3MapToT[Option[Int]] { (x, y, z) => Some(z) }
- x
- }
- def timeTraitMapToInt(reps: Int) = run(reps)(traitMapToInt)
- def traitMapToInt = {
- val goal = ints.clone
- val w = new ArrayWrapper(goal)
- val x = w.traitMapToInt(
- new IntFunc {
- def apply(x: Int, y: Int, z: Int): Int = z + 1
- }
- )
- x
- }
- def timeTraitMapToT(reps: Int) = run(reps)(traitMapToT)
- def traitMapToT = {
- val goal = ints.clone
- val w = new ArrayWrapper(goal)
- val x = w.traitMapToT[Option[Int]](
- new IntFunc2[Option[Int]] {
- def apply(x: Int, y: Int, z:Int): Option[Int] = Some(z)
- }
- )
- x
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement