[R] Vector searching and counting speed optimization

Richard M. Heiberger rmh at temple.edu
Wed May 3 21:02:23 CEST 2006


sum() looks faster to me

tmp <- as.logical(rbinom(100,1,.5))

> system.time(for (i in 1:10000) length(tmp[tmp]))
[1] 0.09 0.00 0.09   NA   NA
> system.time(for (i in 1:10000) sum(tmp))
[1] 0.03 0.00 0.03   NA   NA

I am running on Windows XP

> Sys.getenv()[20]
                            PROCESSOR_IDENTIFIER 
"x86 Family 15 Model 2 Stepping 4, GenuineIntel" 
>




More information about the R-help mailing list