Skip to content

Instantly share code, notes, and snippets.

@maurolepore
Last active June 6, 2024 18:35
Show Gist options
  • Save maurolepore/f9ecf8e8f90b8c9dcf94e02984206bc6 to your computer and use it in GitHub Desktop.
Save maurolepore/f9ecf8e8f90b8c9dcf94e02984206bc6 to your computer and use it in GitHub Desktop.
f <- function(x) Sys.sleep(x)

# takes 3 seconds always
system.time(f(3))
#>    user  system elapsed 
#>   0.000   0.000   3.003

system.time(f(3))
#>    user  system elapsed 
#>   0.017   0.003   3.021

fm <- memoise::memoise(f)

# takes 3 seconds only the first time
system.time(fm(3))
#>    user  system elapsed 
#>   0.001   0.001   3.004

# subsequent calls take virtually no time
system.time(fm(3))
#>    user  system elapsed 
#>   0.028   0.000   0.029

Created on 2024-06-06 with reprex v2.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment