Advertisement
Guest User

Untitled

a guest
Dec 20th, 2009
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.06 KB | None | 0 0
  1. object Benchmark2 {
  2.   val MinArraySize = 5
  3.   val ArraySize = 20
  4.   val WarmupTimes = 10000
  5.   val BenchmarkTimes = 10000
  6.  
  7.   def main(args: Array[String]): Unit = {
  8.     args(0).toInt match {
  9.       case 1 =>
  10.         benchmark("Monomorphic Manifest Int") {
  11.           manifestBenchmark(ArraySize)(x => x)
  12.         }
  13.      
  14.       case 2 =>
  15.         benchmark("Monomorphic Manifest Double") {
  16.           manifestBenchmark(ArraySize)(x => x.toDouble)
  17.         }
  18.  
  19.       case 3 =>
  20.         benchmark("Monomorphic Manifest String") {
  21.           manifestBenchmark(ArraySize)(x => (ArraySize + x).toString)
  22.         }
  23.  
  24.       case 4 =>
  25.         benchmark("Monomorphic Reflection Int") {
  26.           reflectionBenchmark(ArraySize)(x => x)
  27.         }
  28.  
  29.       case 5 =>
  30.         benchmark("Monomorphic Reflection Double") {
  31.           reflectionBenchmark(ArraySize)(x => x.toDouble)
  32.         }
  33.  
  34.       case 6 =>
  35.         benchmark("Monomorphic Reflection String") {
  36.           reflectionBenchmark(ArraySize)(x => (x + ArraySize).toString)
  37.         }
  38.      
  39.       case 7 =>
  40.         // Ensure megamorphic call sites
  41.         repeat(WarmupTimes) {
  42.           manifestBenchmark(MinArraySize)(x => x)
  43.           manifestBenchmark(MinArraySize)(x => x.toDouble)
  44.           manifestBenchmark(MinArraySize)(x => (ArraySize + x).toString)
  45.         }
  46.  
  47.         benchmark("Megamorphic Manifest Int") {
  48.           manifestBenchmark(ArraySize)(x => x)
  49.         }
  50.  
  51.       case 8 =>
  52.         // Ensure megamorphic call sites
  53.         repeat(WarmupTimes) {
  54.           manifestBenchmark(MinArraySize)(x => x)
  55.           manifestBenchmark(MinArraySize)(x => x.toDouble)
  56.           manifestBenchmark(MinArraySize)(x => (ArraySize + x).toString)
  57.         }
  58.  
  59.         benchmark("Megamorphic Manifest Double") {
  60.           manifestBenchmark(ArraySize)(x => x.toDouble)
  61.         }
  62.  
  63.       case 9 =>
  64.         // Ensure megamorphic call sites
  65.         repeat(WarmupTimes) {
  66.           manifestBenchmark(MinArraySize)(x => x)
  67.           manifestBenchmark(MinArraySize)(x => x.toDouble)
  68.           manifestBenchmark(MinArraySize)(x => (ArraySize + x).toString)
  69.         }
  70.  
  71.         benchmark("Megamorphic Manifest String") {
  72.           manifestBenchmark(ArraySize)(x => (ArraySize + x).toString)
  73.         }
  74.  
  75.       case 10 =>
  76.         // Ensure megamorphic call sites
  77.         repeat(WarmupTimes) {
  78.           reflectionBenchmark(MinArraySize)(x => x)
  79.           reflectionBenchmark(MinArraySize)(x => x.toDouble)
  80.           reflectionBenchmark(MinArraySize)(x => (ArraySize + x).toString)
  81.         }
  82.  
  83.         benchmark("Megamorphic Reflection Int") {
  84.           reflectionBenchmark(ArraySize)(x => x)
  85.         }
  86.  
  87.       case 11 =>
  88.         // Ensure megamorphic call sites
  89.         repeat(WarmupTimes) {
  90.           reflectionBenchmark(MinArraySize)(x => x)
  91.           reflectionBenchmark(MinArraySize)(x => x.toDouble)
  92.           reflectionBenchmark(MinArraySize)(x => (ArraySize + x).toString)
  93.         }
  94.  
  95.         benchmark("Megamorphic Reflection Double") {
  96.           reflectionBenchmark(ArraySize)(x => x.toDouble)
  97.         }
  98.  
  99.       case 12 =>
  100.         // Ensure megamorphic call sites
  101.         repeat(WarmupTimes) {
  102.           reflectionBenchmark(MinArraySize)(x => x)
  103.           reflectionBenchmark(MinArraySize)(x => x.toDouble)
  104.           reflectionBenchmark(MinArraySize)(x => (ArraySize + x).toString)
  105.         }
  106.  
  107.         benchmark("Megamorphic Reflection String") {
  108.           reflectionBenchmark(ArraySize)(x => (ArraySize + x).toString)
  109.         }
  110.     }
  111.   }
  112.  
  113.   def reflectionBenchmark[T](size: Int)(f: Int => T)(implicit m: ClassManifest[T], o: Ordering[T]): Unit = {
  114.     val array = reflectionCreateArray(size)(f)
  115.     reflectionSort(array)
  116.     reflectionEnsureSorted(array)
  117.   }
  118.  
  119.   def manifestBenchmark[T](size: Int)(f: Int => T)(implicit m: ClassManifest[T], o: Ordering[T]): Unit = {
  120.     val array = manifestCreateArray(size)(f)
  121.     manifestSort(array)
  122.     manifestEnsureSorted(array)
  123.   }
  124.  
  125.   def reflectionCreateArray[T](size: Int)(f: Int => T)(implicit m: ClassManifest[T]): Array[T] = {
  126.     val array = new Array[T](size)
  127.     for (i <- 0 until size) {
  128.       array(i) = f(size - i)
  129.     }
  130.     array
  131.   }
  132.  
  133.   def manifestCreateArray[T](size: Int)(f: Int => T)(implicit m: ClassManifest[T]): Array[T] = {
  134.     val array = new Array[T](size)
  135.     for (i <- 0 until size) {
  136.       m.arrayUpdate(array, i, f(size - i))
  137.     }
  138.     array
  139.   }
  140.  
  141.   def reflectionSort[T](array: Array[T])(implicit o: Ordering[T]): Unit = {
  142.     for (i <- 1 until array.length) {
  143.       var j = i
  144.       val value = array(j)
  145.       while (j > 0 && o.compare(array(j - 1), value) > 0) {
  146.         array(j) = array(j - 1)
  147.         j -= 1
  148.       }
  149.       array(j) = value
  150.     }
  151.   }
  152.  
  153.   def manifestSort[T](array: Array[T])(implicit m: ClassManifest[T], o: Ordering[T]): Unit = {
  154.     for (i <- 1 until array.length) {
  155.       var j = i
  156.       val value = m.arrayApply(array, j)
  157.       while (j > 0 && o.compare(m.arrayApply(array, j - 1), value) > 0) {
  158.         m.arrayUpdate(array, j, m.arrayApply(array, j - 1))
  159.         j -= 1
  160.       }
  161.       m.arrayUpdate(array, j, value)
  162.     }
  163.   }
  164.  
  165.   def reflectionEnsureSorted[T](array: Array[T])(implicit o: Ordering[T]): Unit = {
  166.     for (i <- 1 until array.length) {
  167.       require(o.compare(array(i), array(i - 1)) >= 0)
  168.     }
  169.   }
  170.  
  171.   def manifestEnsureSorted[T](array: Array[T])(implicit m: ClassManifest[T], o: Ordering[T]): Unit = {
  172.     for (i <- 1 until array.length) {
  173.       require(o.compare(m.arrayApply(array, i), m.arrayApply(array, i - 1)) >= 0)
  174.     }
  175.   }
  176.  
  177.   def repeat(times: Int)(block: => Unit): Unit = {
  178.     var i = 0
  179.     while (i < times) {
  180.       block
  181.       i += 1
  182.     }
  183.   }
  184.  
  185.   def time(times: Int)(block: => Unit): Double = {
  186.     val start = System.nanoTime
  187.     repeat(times) {
  188.       block
  189.     }
  190.     (System.nanoTime - start) / 1000000.0    
  191.   }
  192.  
  193.   def benchmark(test: String)(block: => Unit): Unit = {
  194.     // warmup
  195.     repeat(WarmupTimes) {
  196.       block
  197.     }
  198.  
  199.     // benchmark
  200.     val elapsed = time(BenchmarkTimes) {
  201.       block
  202.     }
  203.     println(test + " took: " + elapsed + "ms")
  204.  
  205.   }
  206. }
  207.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement