Groovy web console

subscribe to the feed Subscribe
to this
site

Memoization for recursive closures

Published 1 decade ago by Michail Antonov with tags memoization recursion
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
def startTime = System.currentTimeMillis()

def fact = {it -> sleep(100); it ?  it * call(it - 1g) : 1g}
fact = fact.memoize()
println fact(70)
println fact(71)

println ((System.currentTimeMillis() - startTime) / 1000.0)