Published 2 months ago
by
Ricardo

def number = 20 def DYNAMIC_PROGRAMMING = true def fib(n) { n < 2 ? n : fib(n - 2) + fib(n - 1) } def fastFib; def fiboClosure = { BigInteger n -> n < 2 ? n : fastFib(n - 2) + fastFib(n - 1) } if(DYNAMIC_PROGRAMMING){ fastFib = fiboClosure.memoize() }else{ fastFib = fiboClosure } def measure = { closure -> start = System.currentTimeMillis() closure.call() now = System.currentTimeMillis() now - start } def duration = measure { println fiboClosure(number) } println "Calculation of fib(${number}) took ${duration}ms"