DEV Community

Fumiya Yamanaka
Fumiya Yamanaka

Posted on


[Swift] What I learned about Memoization

This post is originally created on May and published on my personal blog.
However, I changed my mind. I noticed that publishing it on the platform is better than on mine because someone can reach out whenever.

The terminology of 'memoization' is very impressive and motivated me to learn new things. Today, I want to share a good effect and show an example. I learned by Javascript in the class so I will try use Swift.

What is Memoization

According to Wikipedia, memoization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again. Memoization has also been used in other contexts (and for purposes other than speed gains), such as in simple mutually recursive descent parsing.

Sometimes, we use the recursive function to simplify the complicated algorithm.

However, it may become the reason to slow down. Then, you need to change the algorithm or use memoization.


For example, there is one of the famous algorithms 'fibonacci sequence'. You'll see the simple code below.

func fibonacci(_ n: Int) -> Int {
  n < 2 ? n : fibonacci(n-1) + fibonacci(n-2)
Enter fullscreen mode Exit fullscreen mode

This is the most simple solution. If given number is more than 2, calculate the sum of the last two number.

If you are interested in more details about fibonacci, please search yourself.

On the other hand, let's code with memoization.

var cache = [Int: Int]()
func cachedFibonacci(_ n: Int) -> Int {
  if let v = cache[n] {
    return v

  let newValue = n < 2 ? n : cachedFibonacci(n-1) + cachedFibonacci(n-2)
  cache[n] = newValue
  return newValue
Enter fullscreen mode Exit fullscreen mode

This example code is quiet manual but very useful.

Comparing methods

we can measure time since Jan, 1, 2001 to use CFAbsoluteTimeGetCurrent in Xcode playground.

At this time, it is enable to get the difference to call this function with calling fibonacci function before and after, and calculate the gap of them.

let start1 = CFAbsoluteTimeGetCurrent()
let diff1 = CFAbsoluteTimeGetCurrent() - start1

// 0.05279099941253662

let start2 = CFAbsoluteTimeGetCurrent()
let diff2 = CFAbsoluteTimeGetCurrent() - start2

// 0.00033092498779296875
Enter fullscreen mode Exit fullscreen mode

Obviously, time with memoization is 100 times faster than the one without memoization.

This is the reason why we should implement the algorithm.


Today, I introduced about memoization with using fibonacci sequence.

Actually when measured the time gap, you can see the necessarily.

If you faced to the issue like slow functions, I hope you recall this article.

Thank you! Enjoy your coding.


Memoization - Wikipedia

Using memoization to speed up slow functions

Measuring execution speed using CFAbsoluteTimeGetCurrent()

Top comments (0)

An Animated Guide to Node.js Event Loop

Node.js doesnโ€™t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.