Ad space (banner)
๐Ÿ”ทVB.NET Lessons
Lesson 38 / 68

Speeding up calculation with memoization (caching)

This lesson covers memoization in VB.NET, with the aim of understanding how to avoid repeating the same calculation.

Memoization stores a result once calculated and, when the same input arrives again, returns the stored result rather than recalculating. A Dictionary(Of TKey, TValue) holds the results.

The sample code stores computed results in a Dictionary called cache and checks cache.ContainsKey(n) each time SlowSquare() is called. From the second identical input onwards you see the "Retrieved from cache" message and the calculation is skipped.

A common early stumble is deciding when caching is worth it. Memoizing cheap work can cost more in cache management than it saves, so keep it for genuinely slow operations.

In professional work, memoization and caching are commonly applied to expensive, repeatable work — heavy calculations, results fetched from an API.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run VB.NET directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)