Taking a Look at Groovy @Memoized Functions @WorldOfZeroDevelopment
Taking a Look at Groovy @Memoized Functions  @WorldOfZeroDevelopment
Uploaded April 2018 | Updated November 2026, 4 days ago
Reading up on some of the features of Groovy I stumbled upon the @Memoized annotation. This method annotation allows you to tell Groovy to memoize the results from a function. What is memoizing? Think of it as caching the results of a complex computation.

For example the Fibonacci sequence requires us to recursively calculate f(n) = f(n - 1) + f(n - 2). In order to calculate fibonacci for f(100) this means we need to calculate f(1) a hundred times! Why? The answer is going to be the same every time. The idea of memoizing is to take the results of the complex function and store the results after the first computation into a data structure that makes it easier to lookup. For fibonacci this might be as simple as an array with the results of f(n) stored at index n in our array. The general idea being to replace a complex or slow function with a relatively quick table lookup.

In Groovy we get memoization for free as a feature of the language. By adding @Memoized to a function we can tell Groovy to cache the results of a function for the given inputs and use that cache when the same inputs are used again. In our tests we dropped the time of computing the 40th fibonacci number from over 7 seconds (for 20 computations) to 60 milliseconds. An increase of more than 100 times! And it only took us a single line of code to get this advantage too. That's a lot of power for a single line of code.

The code from this video is available on GitHub here: gist.github.com/runewake2/2d63de01f53619d2d5b976043dc12420

More on Groovy at groovy-lang.org

The Groovy documentation on @Memoized, including help on managing the backing cache and additional examples: docs.groovy-lang.org/latest/html/gapi/groovy/transform/Memoized.html

Wikipedia's article on Time Complexity and what it means (don't let some of the more obscure terms intimidate you, focus on constant, logarithmic, linear, exponential and factorial time complexity): en.wikipedia.org/wiki/Time_complexity

The World of Zero Discord channel: discord.gg/hU5Kq2u
Taking a Look at Groovy @Memoized FunctionsBuilding OBS Overlays in Unity 3DAnimating the Elevator - An Elevator Between Scenes - Part 1Improving the Voxel Cube with Lighting and Optimized UpdatesExpanding our Mindustry Server with Custom Maps, Saves and MoreBuilding a Custom Unity Terrain ShaderBuilding a Static Website from Markdown with HugoI Learn About Stencil Buffers and Cutting Holes in ThingsBuilding an Editor Window - Unity Observer - Part 0Building a Cloud Game w/ @MicrosoftReactor - Day 2Prototyping the Sand Table - Sand Table - Part 1Pseudo-Random Colors with Hashed Strings
World of Zero |

Taking a Look at Groovy @Memoized Functions

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER