R:为什么[[]]的方法为列表设置子集比使用$更快? [英] R: Why is the [[ ]] approach for subsetting a list faster than using $?

查看:54
本文介绍了R:为什么[[]]的方法为列表设置子集比使用$更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在从事一些需要我进行大量列表子设置的项目,并且在分析代码时,我意识到使用object [["nameHere"]]子集列表的方法通常比object $更快.名称这里的方法.

I've been working on a few projects that have required me to do a lot of list subsetting and while profiling code I realised that the object[["nameHere"]] approach to subsetting lists was usually faster than the object$nameHere approach.

作为示例,如果我们创建一个包含命名组件的列表:

As an example if we create a list with named components:

a.long.list <- as.list(rep(1:1000))
names(a.long.list) <- paste0("something",1:1000)

这是为什么:

system.time (
for (i in 1:10000) {
    a.long.list[["something997"]]
}
)


user  system elapsed 
0.15    0.00    0.16 

比这更快:

system.time (
    for (i in 1:10000) {
        a.long.list$something997
    }
)

user  system elapsed 
0.23    0.00    0.23 

我的问题仅仅是这种行为是否普遍适用,我应该尽可能避免使用$子集,或者最有效的选择是否取决于其他因素?

My question is simply whether this behaviour is true universally and I should avoid the $ subset wherever possible or does the most efficient choice depend on some other factors?

推荐答案

函数[[首先遍历所有元素以尝试完全匹配,然后然后尝试进行部分匹配. $函数依次尝试对每个元素进行完全匹配和部分匹配.如果执行:

Function [[ first goes through all elements trying for exact match, then tries to do partial match. The $ function tries both exact and partial match on each element in turn. If you execute:

system.time (
    for (i in 1:10000) {
     a.long.list[["something9973", exact=FALSE]]
     }
)

即,您正在运行不完全匹配的部分匹配,您会发现$实际上要快得多.

i.e., you are running a partial match where there is no exact match, you will find that $ is in fact ever so slightly faster.

这篇关于R:为什么[[]]的方法为列表设置子集比使用$更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆