Skip to content

Instantly share code, notes, and snippets.

@bodiam
Created July 29, 2014 20:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bodiam/2ab1b98a1abb33f5ba1b to your computer and use it in GitHub Desktop.
Save bodiam/2ab1b98a1abb33f5ba1b to your computer and use it in GitHub Desktop.
Using a Groovy Trait to make exceptions usable for flow control.
@Grab(group='org.gperfutils', module='gbench', version='0.4.2-groovy-2.1')
import java.lang.String
trait NoStackTrace {
Throwable fillInStackTrace() {
return this
}
}
class ExpensiveException extends RuntimeException { }
class CheapException extends RuntimeException implements NoStackTrace {}
def r = benchmark {
'Cheap Exception' {
def e = new CheapException()
}
'Expensive Exception' {
def e = new ExpensiveException()
}
}
r.prettyPrint()
/**
* Environment
===========
* Groovy: 2.3.6
* JVM: Java HotSpot(TM) 64-Bit Server VM (25.0-b70, Oracle Corporation)
* JRE: 1.8.0
* Total Memory: 231 MB
* Maximum Memory: 3641 MB
* OS: Mac OS X (10.9.3, x86_64)
Options
=======
* Warm Up: Auto (- 60 sec)
* CPU Time Measurement: On
user system cpu real
Cheap Exception 36 0 36 37
Expensive Exception 7621 2 7623 7625
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment