Created
August 2, 2013 14:09
-
-
Save melix/6140171 to your computer and use it in GitHub Desktop.
Benchmark for Fibonacci 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(group='org.gperfutils', module='gbench', version='0.4.2-groovy-2.1') | |
import groovy.transform.CompileStatic | |
import groovy.transform.Memoized | |
def untypedFib(n) { | |
n<2?n:untypedFib(n-1)+untypedFib(n-2) | |
} | |
int typedFib(int n) { | |
n<2?n:typedFib(n-1)+typedFib(n-2) | |
} | |
@CompileStatic | |
int csFib(int n) { | |
n<2?n:csFib(n-1)+csFib(n-2) | |
} | |
@Memoized | |
def memoizedUntypedFib(n) { | |
n<2?n:memoizedUntypedFib(n-1)+memoizedUntypedFib(n-2) | |
} | |
@Memoized | |
@CompileStatic | |
int memoizedCsFib(int n) { | |
n<2?n:memoizedCsFib(n-1)+memoizedCsFib(n-2) | |
} | |
def r = benchmark { | |
'Untyped' { untypedFib(40) } | |
'Typed' { typedFib(40) } | |
'CompileStatic' { csFib(40) } | |
'Memoized' { memoizedUntypedFib(40) } | |
'Memoized CompileStatic' { memoizedCsFib(40) } | |
} | |
r.prettyPrint() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results here: