Description Usage Arguments Value See Also Examples
tic
- Starts the timer and stores the start time and
the message on the stack.
toc
- Notes the current timer and computes elapsed
time since the matching call to tic()
. When
quiet
is FALSE
, prints the associated message
and the elapsed time.
toc.outmsg
- Formats a message for pretty printing.
Redefine this for different formatting.
tic.clearlog
- Clears the tic/toc log.
tic.clear
- Clears the tic/toc stack. This could be
useful in cases when because of an error the closing toc()
calls never get executed.
tic.log
- Returns log messages from calls to tic/toc
since the last call to tic.clearlog
.
1 2 3 4 5 6 7 8 9 10 11 |
msg |
- a text string associated with the timer. It
gets printed on a call to |
func.tic |
Function producing the formatted message
with a signature |
... |
The other parameters that are passed to
|
log |
- When |
quiet |
When |
func.toc |
Function producing the formatted message
with a signature |
tic |
Time from the call to tic()
( |
toc |
Time from the call to toc()
( |
format |
When true, |
tic
returns the timestamp (invisible).
toc
returns an (invisible) list containing the
timestamps tic
, toc
, and the message
msg
.
toc.outmsg
returns formatted message.
tic.log
returns a list of formatted messages
(format = TRUE
) or a list of lists containing the
timestamps and unformatted messages from prior calls to
tic/toc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | ## Not run:
## Basic use case
tic()
print("Do something...")
Sys.sleep(1)
toc()
# 1.034 sec elapsed
## Inline timing example, similar to system.time()
tic(); for(i in 1:1000000) { j = i / 2 }; toc()
# 0.527 sec elapsed
## Timing multiple steps
tic("step 1")
print("Do something...")
Sys.sleep(1)
toc()
# step 1: 1.005 sec elapsed
tic("step 2")
print("Do something...")
Sys.sleep(1)
toc()
# step 2: 1.004 sec elapsed
## Timing nested code
tic("outer")
Sys.sleep(1)
tic("middle")
Sys.sleep(2)
tic("inner")
Sys.sleep(3)
toc()
# inner: 3.004 sec elapsed
toc()
# middle: 5.008 sec elapsed
toc()
# outer: 6.016 sec elapsed
## Timing in a loop and analyzing the results later using tic.log().
tic.clearlog()
for (x in 1:10)
{
tic(x)
Sys.sleep(1)
toc(log = TRUE, quiet = TRUE)
}
log.txt <- tic.log(format = TRUE)
log.lst <- tic.log(format = FALSE)
tic.clearlog()
timings <- unlist(lapply(log.lst, function(x) x$toc - x$tic))
mean(timings)
# [1] 1.001
writeLines(unlist(log.txt))
# 1: 1.002 sec elapsed
# 2: 1 sec elapsed
# 3: 1.002 sec elapsed
# 4: 1.001 sec elapsed
# 5: 1.001 sec elapsed
# 6: 1.001 sec elapsed
# 7: 1.001 sec elapsed
# 8: 1.001 sec elapsed
# 9: 1.001 sec elapsed
# 10: 1 sec elapsed
## Using custom callbacks in tic/toc
my.msg.tic <- function(tic, msg)
{
if (is.null(msg) || is.na(msg) || length(msg) == 0)
{
outmsg <- paste(round(toc - tic, 3), " seconds elapsed", sep="")
}
else
{
outmsg <- paste("Starting ", msg, "...", sep="")
}
}
my.msg.toc <- function(tic, toc, msg, info)
{
if (is.null(msg) || is.na(msg) || length(msg) == 0)
{
outmsg <- paste(round(toc - tic, 3), " seconds elapsed", sep="")
}
else
{
outmsg <- paste(info, ": ", msg, ": ",
round(toc - tic, 3), " seconds elapsed", sep="")
}
}
tic("outer", quiet = FALSE, func.tic = my.msg.tic)
# Starting outer...
Sys.sleep(1)
tic("middle", quiet = FALSE, func.tic = my.msg.tic)
# Starting middle...
Sys.sleep(2)
tic("inner", quiet = FALSE, func.tic = my.msg.tic)
Sys.sleep(3)
# Starting inner...
toc(quiet = FALSE, func.toc = my.msg.toc, info = "INFO")
# INFO: inner: 3.005 seconds elapsed
toc(quiet = FALSE, func.toc = my.msg.toc, info = "INFO")
# INFO: middle: 5.01 seconds elapsed
toc(quiet = FALSE, func.toc = my.msg.toc, info = "INFO")
# INFO: outer: 6.014 seconds elapsed
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.