Generally, when I run benchmarks, I wrap my statements in expression
.
Recently, it was suggested to either (a) not do so or (b) use quote
instead of expression.
I find two advantages to wrapping the statements:
- compared to entire statements, they are more easily swapped out.
- I can lapply over a list of inputs, and compare those results
However, in exploring the different methods, I noticed a discrepency between the three methods (wrapping in expression
, wrapping in quote
, or not wrapping at all)
The question is:
Why the discrepency?
(it appears that wrapping in quote
does not actually evaluate the call.)
EXAMPLE:
# SAMPLE DATA
mat <- matrix(sample(seq(1e6), 4^2*1e4, T), ncol=400)
# RAW EXPRESSION TO BENCHMARK IS:
# apply(mat, 2, mean)
# WRAPPED EXPRESSION:
expr <- expression(apply(mat, 2, mean))
quot <- quote(apply(mat, 2, mean))
# BENCHMARKS
benchmark(raw=apply(mat, 2, mean), expr, quot)[, -(7:8)]
# test replications elapsed relative user.self sys.self
# 2 expr 100 1.269 NA 1.256 0.019
# 3 quot 100 0.000 NA 0.001 0.000
# 1 raw 100 1.494 NA 1.286 0.021
# BENCHMARKED INDIVIDUALLY
benchmark(raw=apply(mat, 2, mean))[, -(7:8)]
benchmark(expr)[, -(7:8)]
benchmark(quot)[, -(7:8)]
# results
# test replications elapsed relative user.self sys.self
# 1 raw 100 1.274 1 1.26 0.018
# test replications elapsed relative user.self sys.self
# 1 expr 100 1.476 1 1.342 0.021
# test replications elapsed relative user.self sys.self
# 1 quot 100 0.006 1 0.006 0.001
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…