[R] convert to binary to decimal

Henrik Bengtsson hb at stat.berkeley.edu
Sun Feb 18 02:14:45 CET 2007


On 2/16/07, Jim Regetz <regetz at nceas.ucsb.edu> wrote:
> Roland Rau wrote:
> > On 2/16/07, Petr Pikal <petr.pikal at precheza.cz> wrote:
> >> Hi
> >>
> >> slight modification of your function can be probably even quicker:
> >>
> >> fff<-function(x) sum(2^(which(rev(x))-1))
> >> :-)
> >> Petr
> >>
> >>
> > Yes, your function is slightly but consistently faster than my suggestion.
> > But my "tests" show still Bert Gunter's function to be far ahead of the
> > rest.
> >
>
> Mere trifling at this point, but here's a tweak that yields slightly
> faster performance on my system (with a caveat):
>
> # Bert's original function
> bert.gunter <- function(x) {
>   sum(x * 2^(rev(seq_along(x)) - 1))
> }
>
> # A slightly modified function
> dead.horse <- function(x) {
>   sum( 2^(rev(seq_along(x))-1)[x] )
> }
>
> set.seed(1)
> huge.list <- replicate(20000,
>          sample(c(TRUE,FALSE), 20, replace=TRUE), simplify=FALSE)
> horse.time <- replicate(15, system.time(lapply(huge.list, dead.horse)))
> bert.time <- replicate(15, system.time(lapply(huge.list, bert.gunter)))
>
>
> # Print mean times (exclude first 2 to improve consistency)
> > rowMeans(bert.time[, -(1:2)])
> [1] 0.618600 0.000867 0.621000 0.000000 0.000000
> > rowMeans(horse.time[, -(1:2)])
> [1] 0.580286 0.000571 0.582143 0.000000 0.000000
>
> Hope no one comes along to beat this function ;-)

Why not? ;)

jumpy.roo <- function(x) { sum(2^.subset((length(x)-1):0, x)) }

horse.time <- replicate(15, system.time(lapply(huge.list, dead.horse)))
bert.time <- replicate(15, system.time(lapply(huge.list, bert.gunter)))
roo.time <- replicate(15, system.time(lapply(huge.list, jumpy.roo)))

> rowMeans(bert.time[, -(1:2)])
[1] 0.6169231 0.0000000 0.6176923        NA        NA
> rowMeans(horse.time[, -(1:2)])
[1] 0.604615385 0.001538462 0.603846154          NA          NA
> rowMeans(roo.time[, -(1:2)])
[1] 0.2030769 0.0000000 0.2092308        NA        NA

You can push it further if you allow:

jumpy.redroo <- function(x) .Internal(sum(2^.subset((length(x)-1):0, x)))

redroo.time <- replicate(15, system.time(lapply(huge.list, jumpy.redroo)))
> rowMeans(redroo.time[, -(1:2)])
[1] 0.1653846 0.0000000 0.1646154        NA        NA

which is 3-4 faster than "bert.gunter".

Cheers

Henrik


> Incidentally, I generated huge.list by randomly sampling TRUE and FALSE
> values with equal probability. I believe this matches what Roland did,
> and it seems quite reasonable given that the vectors are meant to
> represent binary numbers. But FWIW, as the vectors get more densely
> populated with TRUE values, dead.horse() loses its advantage. In the
> limit, if all values are TRUE, Bert's multiplication is slightly faster
> than logical indexing.
>
> Fun on a Friday...
>
> Cheers,
> Jim
>
> ------------------------------
> James Regetz, Ph.D.
> Scientific Programmer/Analyst
> National Center for Ecological Analysis & Synthesis
> 735 State St, Suite 300
> Santa Barbara, CA 93101
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list