考虑一下守则:
a=runif(1000)
microbenchmark::microbenchmark(order(a,method="radix"))
microbenchmark::microbenchmark(sort.list(a,method="radix"))运行这段代码,我看到order()比sort.list()更好的性能。另一方面,如果我使用100000的样本大小,这两个函数的性能几乎相同。
为什么会发生这种事?
发布于 2017-10-17 12:55:31
看看函数sort.list(),它调用了order() ie。当这个部分相当于时间密集型部分(大向量)时,它们将是相同的:
> base::sort.list
function (x, partial = NULL, na.last = TRUE, decreasing = FALSE,
method = c("shell", "quick", "radix"))
{
if (is.integer(x) || is.factor(x))
method <- "radix"
method <- match.arg(method)
if (!is.atomic(x))
stop("'x' must be atomic for 'sort.list'\nHave you called 'sort' on a list?")
if (!is.null(partial))
.NotYetUsed("partial != NULL")
if (method == "quick") {
if (is.factor(x))
x <- as.integer(x)
if (is.numeric(x))
return(sort(x, na.last = na.last, decreasing = decreasing,
method = "quick", index.return = TRUE)$ix)
else stop("method = \"quick\" is only for numeric 'x'")
}
if (is.na(na.last)) {
x <- x[!is.na(x)]
na.last <- TRUE
}
if (method == "radix") {
return(order(x, na.last = na.last, decreasing = decreasing,
method = "radix"))
}
.Internal(order(na.last, decreasing, x))
}https://stackoverflow.com/questions/46790490
复制相似问题