性能主题

Java 7与Groovy 2.1性能比较

  测试比较: Java 1.7.0_25 and Groovy 2.1.6.

Java测试代码:

@Override
Integer compute() {
  def size = end - start
  if (size == 1) {
    Math.max(array[start], array[end])
  } else {
    int diff = size / 2
    MaxValueSeeker left =
      new MaxValueSeeker(array, start, start + diff)
    left.fork()
    MaxValueSeeker right =
      new MaxValueSeeker(array, start + diff, end)
    Math.max(right.compute(), left.join())
  }
}

groovy测试代码:

def int proceed(int reps) {
  List<GroovyPojo> list = new ArrayList<>()
  int sum = 0;
  reps.times {
    // first param is int and second is String
    list.add(new GroovyPojo(value: it, stringValue: it))
  }
  list.each {
    if (Integer.parseInt(it.stringValue) == it.value) {
      sum += it.value
    }
  }
  sum
}

测试结果:


Method Java [ns] Groovy [ns] Factor
Fork/Join 22.132 181.018 8.18
Pojos 117.914 856.337 7.26
Quicksort 68.728 330.159 4.80
Quicksort with @CompileStatic 67.752 147.792 2.18

 

Play框架和Grails对比