Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object Benchmark2 {
- val MinArraySize = 5
- val ArraySize = 20
- val WarmupTimes = 10000
- val BenchmarkTimes = 10000
- def main(args: Array[String]): Unit = {
- args(0).toInt match {
- case 1 =>
- benchmark("Monomorphic Manifest Int") {
- manifestBenchmark(ArraySize)(x => x)
- }
- case 2 =>
- benchmark("Monomorphic Manifest Double") {
- manifestBenchmark(ArraySize)(x => x.toDouble)
- }
- case 3 =>
- benchmark("Monomorphic Manifest String") {
- manifestBenchmark(ArraySize)(x => (ArraySize + x).toString)
- }
- case 4 =>
- benchmark("Monomorphic Reflection Int") {
- reflectionBenchmark(ArraySize)(x => x)
- }
- case 5 =>
- benchmark("Monomorphic Reflection Double") {
- reflectionBenchmark(ArraySize)(x => x.toDouble)
- }
- case 6 =>
- benchmark("Monomorphic Reflection String") {
- reflectionBenchmark(ArraySize)(x => (x + ArraySize).toString)
- }
- case 7 =>
- // Ensure megamorphic call sites
- repeat(WarmupTimes) {
- manifestBenchmark(MinArraySize)(x => x)
- manifestBenchmark(MinArraySize)(x => x.toDouble)
- manifestBenchmark(MinArraySize)(x => (ArraySize + x).toString)
- }
- benchmark("Megamorphic Manifest Int") {
- manifestBenchmark(ArraySize)(x => x)
- }
- case 8 =>
- // Ensure megamorphic call sites
- repeat(WarmupTimes) {
- manifestBenchmark(MinArraySize)(x => x)
- manifestBenchmark(MinArraySize)(x => x.toDouble)
- manifestBenchmark(MinArraySize)(x => (ArraySize + x).toString)
- }
- benchmark("Megamorphic Manifest Double") {
- manifestBenchmark(ArraySize)(x => x.toDouble)
- }
- case 9 =>
- // Ensure megamorphic call sites
- repeat(WarmupTimes) {
- manifestBenchmark(MinArraySize)(x => x)
- manifestBenchmark(MinArraySize)(x => x.toDouble)
- manifestBenchmark(MinArraySize)(x => (ArraySize + x).toString)
- }
- benchmark("Megamorphic Manifest String") {
- manifestBenchmark(ArraySize)(x => (ArraySize + x).toString)
- }
- case 10 =>
- // Ensure megamorphic call sites
- repeat(WarmupTimes) {
- reflectionBenchmark(MinArraySize)(x => x)
- reflectionBenchmark(MinArraySize)(x => x.toDouble)
- reflectionBenchmark(MinArraySize)(x => (ArraySize + x).toString)
- }
- benchmark("Megamorphic Reflection Int") {
- reflectionBenchmark(ArraySize)(x => x)
- }
- case 11 =>
- // Ensure megamorphic call sites
- repeat(WarmupTimes) {
- reflectionBenchmark(MinArraySize)(x => x)
- reflectionBenchmark(MinArraySize)(x => x.toDouble)
- reflectionBenchmark(MinArraySize)(x => (ArraySize + x).toString)
- }
- benchmark("Megamorphic Reflection Double") {
- reflectionBenchmark(ArraySize)(x => x.toDouble)
- }
- case 12 =>
- // Ensure megamorphic call sites
- repeat(WarmupTimes) {
- reflectionBenchmark(MinArraySize)(x => x)
- reflectionBenchmark(MinArraySize)(x => x.toDouble)
- reflectionBenchmark(MinArraySize)(x => (ArraySize + x).toString)
- }
- benchmark("Megamorphic Reflection String") {
- reflectionBenchmark(ArraySize)(x => (ArraySize + x).toString)
- }
- }
- }
- def reflectionBenchmark[T](size: Int)(f: Int => T)(implicit m: ClassManifest[T], o: Ordering[T]): Unit = {
- val array = reflectionCreateArray(size)(f)
- reflectionSort(array)
- reflectionEnsureSorted(array)
- }
- def manifestBenchmark[T](size: Int)(f: Int => T)(implicit m: ClassManifest[T], o: Ordering[T]): Unit = {
- val array = manifestCreateArray(size)(f)
- manifestSort(array)
- manifestEnsureSorted(array)
- }
- def reflectionCreateArray[T](size: Int)(f: Int => T)(implicit m: ClassManifest[T]): Array[T] = {
- val array = new Array[T](size)
- for (i <- 0 until size) {
- array(i) = f(size - i)
- }
- array
- }
- def manifestCreateArray[T](size: Int)(f: Int => T)(implicit m: ClassManifest[T]): Array[T] = {
- val array = new Array[T](size)
- for (i <- 0 until size) {
- m.arrayUpdate(array, i, f(size - i))
- }
- array
- }
- def reflectionSort[T](array: Array[T])(implicit o: Ordering[T]): Unit = {
- for (i <- 1 until array.length) {
- var j = i
- val value = array(j)
- while (j > 0 && o.compare(array(j - 1), value) > 0) {
- array(j) = array(j - 1)
- j -= 1
- }
- array(j) = value
- }
- }
- def manifestSort[T](array: Array[T])(implicit m: ClassManifest[T], o: Ordering[T]): Unit = {
- for (i <- 1 until array.length) {
- var j = i
- val value = m.arrayApply(array, j)
- while (j > 0 && o.compare(m.arrayApply(array, j - 1), value) > 0) {
- m.arrayUpdate(array, j, m.arrayApply(array, j - 1))
- j -= 1
- }
- m.arrayUpdate(array, j, value)
- }
- }
- def reflectionEnsureSorted[T](array: Array[T])(implicit o: Ordering[T]): Unit = {
- for (i <- 1 until array.length) {
- require(o.compare(array(i), array(i - 1)) >= 0)
- }
- }
- def manifestEnsureSorted[T](array: Array[T])(implicit m: ClassManifest[T], o: Ordering[T]): Unit = {
- for (i <- 1 until array.length) {
- require(o.compare(m.arrayApply(array, i), m.arrayApply(array, i - 1)) >= 0)
- }
- }
- def repeat(times: Int)(block: => Unit): Unit = {
- var i = 0
- while (i < times) {
- block
- i += 1
- }
- }
- def time(times: Int)(block: => Unit): Double = {
- val start = System.nanoTime
- repeat(times) {
- block
- }
- (System.nanoTime - start) / 1000000.0
- }
- def benchmark(test: String)(block: => Unit): Unit = {
- // warmup
- repeat(WarmupTimes) {
- block
- }
- // benchmark
- val elapsed = time(BenchmarkTimes) {
- block
- }
- println(test + " took: " + elapsed + "ms")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement