Which of the following is more efficient in terms of time to completion?
one.way = function(n) { if (n) c(n, one.way(n-1)) }
two.way = function(n) { if (n) c(two.way(n-1), n) }
The emphasis here is on which order to combine a singleton vector with another vector whose length is likely greater than one.
How would your answer change if you need to include the time to reverse the vector after constructing it with one of the above functions? It may or may not be known at the time of combination in which order the combined objects are going to be needed.
To answer your question, they're about equal
Use
microbenchmark
to test the performance of two fast functionsNotice the lower-quartile, median, and upper-quartile are all the same. The means are slightly different but it's not significant. See below