Skip to content

Instantly share code, notes, and snippets.

@melix
Created August 2, 2013 14:09
Show Gist options
  • Save melix/6140171 to your computer and use it in GitHub Desktop.
Save melix/6140171 to your computer and use it in GitHub Desktop.
Benchmark for Fibonacci in Groovy
@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()
@melix
Copy link
Author

melix commented Aug 2, 2013

Results here:

===========
* Groovy: #ImplementationVersion#
* JVM: Java HotSpot(TM) 64-Bit Server VM (24.0-b48, Oracle Corporation)
    * JRE: 1.7.0_40-ea
    * Total Memory: 423 MB
    * Maximum Memory: 1765.5 MB
* OS: Linux (3.8.0-22-generic, amd64)

Options
=======
* Warm Up: Auto (- 60 sec)
* CPU Time Measurement: On

WARNING: Timed out waiting for "Untyped" to be stable
                               user    system          cpu         real

Untyped                 38839973668  12285245  38852258913  39092056069
Typed                    1379972834   3061037   1383033871   1383941657
CompileStatic             509985969   3233744    513219713    513529361
Memoized                        209         0          209          212
Memoized CompileStatic          210         0          210          214

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment