Skip to content

Instantly share code, notes, and snippets.

@jimjam-slam
Last active February 13, 2022 23:00
Show Gist options
  • Save jimjam-slam/cdcd36c6ea779fab6932 to your computer and use it in GitHub Desktop.
Save jimjam-slam/cdcd36c6ea779fab6932 to your computer and use it in GitHub Desktop.
Returns the time elapsed since the script was sourced. Great for profiling! Source it at the start of your script, then call run.time() when outputting debug info #rstatstips
# james goldie, climate change research centre, unsw, dec 2015
# run.time: returns the time elapsed since this script was
# sourced in sensible units as a string ('**h**m**s')
script.start.time = Sys.time()
run.time <- function()
{
now = Sys.time()
return(paste0(
as.integer(difftime(now, script.start.time, units = 'hours')),
'h',
as.integer(difftime(now, script.start.time, units = 'mins')) %% 60,
'm',
as.integer(difftime(now, script.start.time, units = 'secs')) %% 60,
's '))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment