10 messages in org.r-project.r-help[R] convert to binary to decimal
FromSent OnAttachments
Martin FeldkircherFeb 15, 2007 7:37 am 
Marc SchwartzFeb 15, 2007 8:09 am 
Roland RauFeb 15, 2007 8:21 am 
Bert GunterFeb 15, 2007 8:52 am 
Roland RauFeb 15, 2007 9:13 am 
Marc SchwartzFeb 15, 2007 9:21 am 
Petr PikalFeb 15, 2007 11:19 pm 
Roland RauFeb 16, 2007 7:52 am 
Jim RegetzFeb 16, 2007 5:44 pm 
Henrik BengtssonFeb 17, 2007 5:14 pm 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:[R] convert to binary to decimalActions...
From:Henrik Bengtsson (hb@stat.berkeley.edu)
Date:Feb 17, 2007 5:14:22 pm
List:org.r-project.r-help

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...