Advertisement
Guest User

Untitled

a guest
May 25th, 2015
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. package geotrellis.benchmark
  2.  
  3. import geotrellis.vector._
  4. import geotrellis.raster._
  5. import geotrellis.raster.op.local._
  6.  
  7. import com.google.caliper.Param
  8.  
  9. import scala.util.Random
  10. import scala.annotation.tailrec
  11. import scala.reflect._
  12.  
  13. trait IntFunc {
  14. def apply(x: Int, y: Int, z: Int): Int
  15. }
  16.  
  17. trait IntFunc2[T] {
  18. def apply(x: Int, y: Int, z: Int): T
  19. }
  20.  
  21. class ArrayWrapper(arr: Array[Int]) {
  22. val size = arr.size
  23.  
  24. def function1MapToInt(f: Int => Int): Array[Int] = {
  25. val result = arr.clone
  26. var i = 0
  27. while(i < size) {
  28. val x = arr(i)
  29. result(i) = f(x)
  30. i += 1
  31. }
  32. result
  33. }
  34.  
  35. def function1MapToT[T: ClassTag](f: Int => T): Array[T] = {
  36. val result = Array.ofDim[T](size)
  37. var i = 0
  38. while(i < size) {
  39. val x = arr(i)
  40. result(i) = f(x)
  41. i += 1
  42. }
  43. result
  44. }
  45.  
  46. def function3MapToInt(f: (Int, Int, Int) => Int): Array[Int] = {
  47. val result = arr.clone
  48. var i = 0
  49. while(i < size) {
  50. val x = arr(i)
  51. result(i) = f(x, x, x)
  52. i += 1
  53. }
  54. result
  55. }
  56.  
  57. def function3MapToT[T: ClassTag](f: (Int, Int, Int) => T): Array[T] = {
  58. val result = Array.ofDim[T](size)
  59. var i = 0
  60. while(i < size) {
  61. val x = arr(i)
  62. result(i) = f(x, x, x)
  63. i += 1
  64. }
  65. result
  66. }
  67.  
  68. def traitMapToInt(f: IntFunc): Array[Int] = {
  69. val result = arr.clone
  70. var i = 0
  71. while(i < size) {
  72. val x = arr(i)
  73. result(i) = f(x, x, x)
  74. i += 1
  75. }
  76. result
  77. }
  78.  
  79. def traitMapToT[T: ClassTag](f: IntFunc2[T]): Array[T] = {
  80. val result = Array.ofDim[T](size)
  81. var i = 0
  82. while(i < size) {
  83. val x = arr(i)
  84. result(i) = f(x, x, x)
  85. i += 1
  86. }
  87. result
  88. }
  89.  
  90. }
  91.  
  92. object BoxingBenchmark extends BenchmarkRunner(classOf[BoxingBenchmark])
  93. class BoxingBenchmark extends OperationBenchmark {
  94. @Param(Array("2048"))
  95. var size: Int = 0
  96.  
  97. var ints: Array[Int] = null
  98.  
  99. override def setUp() {
  100. val len = size * size
  101. ints = init(len)(Random.nextInt)
  102. }
  103.  
  104. def timeFunction1MapToInt(reps: Int) = run(reps)(function1MapToInt)
  105. def function1MapToInt = {
  106. val goal = ints.clone
  107. val w = new ArrayWrapper(goal)
  108. val x = w.function1MapToInt{ z => z + 1 }
  109. x
  110. }
  111.  
  112. def timeFunction1MapToT(reps: Int) = run(reps)(function1MapToT)
  113. def function1MapToT = {
  114. val goal = ints.clone
  115. val w = new ArrayWrapper(goal)
  116. val x = w.function1MapToT[Option[Int]] { z => Some(z) }
  117. x
  118. }
  119.  
  120. def timeFunction3MapToInt(reps: Int) = run(reps)(function3MapToInt)
  121. def function3MapToInt = {
  122. val goal = ints.clone
  123. val w = new ArrayWrapper(goal)
  124. val x = w.function3MapToInt { (x, y, z) => z + 1 }
  125. x
  126. }
  127.  
  128. def timeFunction3MapToT(reps: Int) = run(reps)(function3MapToT)
  129. def function3MapToT = {
  130. val goal = ints.clone
  131. val w = new ArrayWrapper(goal)
  132. val x = w.function3MapToT[Option[Int]] { (x, y, z) => Some(z) }
  133. x
  134. }
  135.  
  136. def timeTraitMapToInt(reps: Int) = run(reps)(traitMapToInt)
  137. def traitMapToInt = {
  138. val goal = ints.clone
  139. val w = new ArrayWrapper(goal)
  140. val x = w.traitMapToInt(
  141. new IntFunc {
  142. def apply(x: Int, y: Int, z: Int): Int = z + 1
  143. }
  144. )
  145. x
  146. }
  147.  
  148. def timeTraitMapToT(reps: Int) = run(reps)(traitMapToT)
  149. def traitMapToT = {
  150. val goal = ints.clone
  151. val w = new ArrayWrapper(goal)
  152. val x = w.traitMapToT[Option[Int]](
  153. new IntFunc2[Option[Int]] {
  154. def apply(x: Int, y: Int, z:Int): Option[Int] = Some(z)
  155. }
  156. )
  157. x
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement