timeCast: Time different approaches to coercion

Description Usage Arguments Value Author(s) References Examples

View source: R/cast.R

Description

This function is a simple test for comparing the time involved in different approaches to coercion. Specifically, we compare three different situations: a) where we do no coercion, b) where we coerce each element in the C code via a C-level cast and c) where we coerce the R numeric vector to an integer and then pass the result to C. We do this for different sizes of vector and repeat the calculations several times to obtain stable estimates. We repeat this within both R and the C routine.

Not unsurpisingly, a) is fastest, b) is next and c) is slowest. This allows us to quantify the cost of casting and of R-level coercion within this particular setup.

Usage

1
timeCast(sizes = 10^(0:7), numReplicates = 20, repeats = 5L)

Arguments

sizes

a vector giving the sizes of the vectors on which to perform the calculations

numReplicates

the number of times to repeat the call to the C-level routine for each given vector.

repeats

the number of times within the C routine to repeat the calculation.

target

“int” or “double” to compare casting to an integer vector or casting to a numeric vector to see if there is any difference.

Value

A data frame with time, vector size, the type of test (i.e. direct, cast and “R coerce” corresponding to scenarios a), b) and c)) and the type of time measured (user, sys, elapsed).

Author(s)

Duncan Temple Lang

References

This timing was inspired by a comment by Lee Edlefsen about the expense of coercion in C and is an attempt to measure this. And also we are curious about the cost of duplicating/copying R objects. I implemented this approach in one of the internal arithmetic routines in R to avoid creating a copy of an integer vector to add it element-wise to a numeric vector.

Examples

1
2
3
4
5
6
7
times = timeCast(repeats = 1)

library(lattice)
xyplot(time ~ n, times, group = operation, type = "l", subset = timing== "elapsed", auto.key = list(columns = 3))

dtimes = timeCast(repeats = 1, target = "double")
xyplot(time ~ n, dtimes, group = operation, type = "l", subset = timing== "elapsed", auto.key = list(columns = 3))

omegahat/RTiming documentation built on May 24, 2019, 1:55 p.m.