El método call de los objetos Closure

Si x es una referencia a una clausura, podemos llamarla usando la sintáxis x() o bien x.call():
generaciondecodigos@nereida:~/Lgroovy/closures$ cat -n call.groovy
     1  #!/usr/bin/env groovy
     2
     3  def benchmark(number, task) {
     4
     5    start = System.currentTimeMillis()
     6
     7      number.times { task.call(it) }
     8
     9    stop = System.currentTimeMillis()
    10
    11    (stop - start)/1.0E03;
    12  }
    13
    14  def num = 10000
    15
    16  if (args.length > 0) {
    17    Scanner s = new Scanner(args[0]);
    18    if (s.hasNextInt()) {
    19      num = s.nextInt();
    20    }
    21  }
    22
    23  floatd = benchmark(num) { (int) it/2 }
    24  intd = benchmark(num) { it.intdiv(2) }
    25
    26  println "float division $floatd s. Integer division: $intd s. Ratio = ${floatd/intd}"
El método currentTimeMillis tiene la siguiente firma:

            public static long currentTimeMillis()

Esto es lo que dice el manual sobre currentTimeMillis:

    Returns the current time in milliseconds. Note that while the unit
    of time of the return value is a millisecond, the granularity of
    the value depends on the underlying operating system and may be
    larger. For example, many operating systems measure time in units
    of tens of milliseconds.

    Returns:
        the difference, measured in milliseconds, between the current
        time and midnight, January 1, 1970 UTC.

Siguen algunas ejecuciones:

generaciondecodigos@nereida:~/Lgroovy/closures$ ./call.groovy
float division 0.343 s. Integer division: 0.046 s. Ratio = 7.4565217391
generaciondecodigos@nereida:~/Lgroovy/closures$ ./call.groovy 100000
float division 1.191 s. Integer division: 0.082 s. Ratio = 14.5243902439
generaciondecodigos@nereida:~/Lgroovy/closures$ ./call.groovy xyz
float division 0.364 s. Integer division: 0.059 s. Ratio = 6.1694915254



Casiano Rodríguez León
2010-04-30