2

I have the following snipet of Groovy code

class Test 
{
//  @groovy.transform.CompileStatic
    static main(args) 
    {
        long start = System.currentTimeMillis()
        def x = "3967"
        println x ==~ /[0-9]+([1379])$/
        println System.currentTimeMillis()-start
    }
}

Which is supposed to test weather x is a number ending in 1,3,7,or 9

I'm using the groovy plugin for eclipse, so when i want to run the code I have a few options, I can either run it as a Groovy Script, or as a Java application. Here are the runtimes.

Groovy Script: 93ms
Java Application: 125ms

But then when I enable Static compilation, this happens

Groovy Script: 0ms
Java Application: 312ms 

I'm confused,
1. I thought compiling to a Java application was supposed to be faster than running Groovy as a script.
2. Why is it that the Groovy script option gets so much faster with static compilation, and the Java one gets longer?

Ryan Stull
  • 1,056
  • 14
  • 35
  • 4
    Groovy, even as a script, is compiled to bytecode. This must be related to how eclipse handles compilation and execution. Have you done any warm up before running the tests? – Will Jul 17 '13 at 18:57
  • Extending @WillP's comment, mimic `Test.groovy` to `Test.java` and run both of them side by side, possibly with `groovyc` and `javac` respectively. – dmahapatro Jul 17 '13 at 19:02
  • Also, consider using `System.nanoTime()` instead of `System.currentTimeMillis()` for more accurate timing. – bdkosher Jul 17 '13 at 19:55
  • And the level of error in measuring a single call of a single thing right after startup of the JVM is going to be immense – tim_yates Jul 17 '13 at 20:38

0 Answers0