Created
April 2, 2013 08:44
-
-
Save melix/5290848 to your computer and use it in GitHub Desktop.
Dynamic properties access in Groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grab('com.googlecode.gbench:gbench:0.4.2-groovy-2.1') | |
class Person { | |
String name | |
int age | |
} | |
def p = new Person(name:'foo', age:10) | |
def pName = 'name' | |
def props = p.properties | |
def r = benchmark { // or new groovyx.gbench.BenchmarkBuilder().run { | |
'properties as map' { | |
p.properties[pName] | |
} | |
'properties as string' { | |
p."$pName" | |
} | |
'cached properties' { | |
props[pName] | |
} | |
'get property' { | |
p.getProperty(pName) | |
} | |
} | |
r.prettyPrint() |
this is strange, I tried it with the groovy console. Perhaps you can add a dummy import below the @grab annotation.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
benchmark {}
syntax doesn't work in Groovy console. How does one fix that? Does the script need to be run by GBench?