Skip to content

Instantly share code, notes, and snippets.

@melix
Created April 2, 2013 08:44
Show Gist options
  • Save melix/5290848 to your computer and use it in GitHub Desktop.
Save melix/5290848 to your computer and use it in GitHub Desktop.
Dynamic properties access in Groovy
@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()
@pledbrook
Copy link

The benchmark {} syntax doesn't work in Groovy console. How does one fix that? Does the script need to be run by GBench?

@melix
Copy link
Author

melix commented Apr 3, 2013

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