Hi Duncan,

Duncan Murdoch wrote:
Duncan Murdoch wrote:
Esmail Bonakdarian wrote:
Hello all,

I have a matrix of bit values.

I compute certain values based on the bits in each row. There may be
*duplicate* entries in the matrix, ie several rows may be identical.
These rows change over time, and identical entries may not be next to
each other.

Computing these values is time consuming, so I would like a way to store a value once it's computed. That way I would be able to look up the value based on a given bitstring (row in the matrix) and avoid having to re-compute the same value.

So, my thought is a hash function based on a bit string - does R have such
a thing? Or is there an alternative approach that would do the same in R?
The lookup should be quick, otherwise the gains from avoiding
recomputing identical values would be lost to some extent.

Environments can be hashed. To use this, you'd convert the bit string into a character string, and use that as the name of a variable in an environment.

For example,

cache <- new.env(hash=TRUE, parent=emptyenv())
 ...
bits <- '0101'
if (exists(bits, cache)) {
  return(get(bits, cache))
} else {
  value <- long.computation()
  assign(bits, value)

Whoops, that assignment should have been in the cache:

       assign(bits, value, envir=cache)
  return(value)
}


Thank you for posting this, I'll have to give it a try.
If I can get this to work it should save some some time
spent on recomputing values.

(Sorry for the delayed reply, I was traveling for the
whole last day - suffering from some reverse jet lag at
the moment :-)

Can anyone else think of other alternatives? Always happy
to learn something new.

Thanks!

Esmail

ps: do you think the conversion to a character string
    before comparison/search will cause a noticeable
    performance hit? I still lack an intuitive feel for
    R the programming language. I'd have more of an idea
    with regular high-level programming languages.


Duncan Murdoch

______________________________________________
R-help@r-project.org 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.

Reply via email to