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:Jim Regetz (reg@nceas.ucsb.edu)
Date:Feb 16, 2007 5:44:05 pm
List:org.r-project.r-help

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 ;-)

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