首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么order()比sort.list()更快?

为什么order()比sort.list()更快?
EN

Stack Overflow用户
提问于 2017-10-17 12:45:47
回答 1查看 243关注 0票数 0

考虑一下守则:

代码语言:javascript
复制
a=runif(1000)
microbenchmark::microbenchmark(order(a,method="radix"))
microbenchmark::microbenchmark(sort.list(a,method="radix"))

运行这段代码,我看到order()sort.list()更好的性能。另一方面,如果我使用100000的样本大小,这两个函数的性能几乎相同。

为什么会发生这种事?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-17 12:55:31

看看函数sort.list(),它调用了order() ie。当这个部分相当于时间密集型部分(大向量)时,它们将是相同的:

代码语言:javascript
复制
> 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))
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46790490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档