[R] Vector searching and counting speed optimization

Eric Archer Eric.Archer at noaa.gov
Wed May 3 21:18:35 CEST 2006


Richard,

Thanks!  I never would've thought of using sum() to count the TRUEs in a 
logical vector, but it makes perfect sense.

Cheers,
e.

Richard M. Heiberger wrote:
> 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" 
>   
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>   


-- 

Eric Archer, Ph.D.
NOAA-SWFSC
8604 La Jolla Shores Dr.
La Jolla, CA 92037
858-546-7121,7003(FAX)
eric.archer at noaa.gov


"Lighthouses are more helpful than churches."
    - Benjamin Franklin

"Cogita tute" - Think for yourself




More information about the R-help mailing list