[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Question of reference and (sub)strings.

Steve [RubyTalk]

12/14/2005 3:32:00 PM

Imagine comparing using C++ and Ruby to manipulate some strings. I would
like to confirm some assumptions I am making about in-memory efficiency
with Ruby. (This explains why the example seems contrived and I don't
want to be told to tackle the problem a different way, such as splitting
up the input as it is read from file/sockets etc... :-) )

Assume I've got a large in-memory sequence of bytes (this would be
represented in C(++) using a malloc block) and in ruby as a String. I
have a pre-defined (non-trivial, potentially computationally expensive)
function which calculates a sequence of offsets into the String subject
to some arbitrary criteria... and subsequently I wish to reference
sub-strings (i.e. strings between two successive offsets) as if they
were independent of the original string (though, of course, each having
a fixed length.) N.B. This could be done 'cheaply' using pointers into
the original string if using C/C++.

Given that I only want to compute the offsets once, an obvious solution
would be to construct an Array of String - each element representing a
sub-string of the original... but this would double memory use. What
would be the best way to avoid duplicating the character sequences and
causing run-time bloat?

By corollary, if I had a large number of Strings, what would be the most
memory efficient way to represent their concatenation? If I had n mK
stings, would I need another n*mK contiguous block of memory to
represent their concatenation?





4 Answers

Adam Shelly

12/15/2005 5:32:00 AM

0

On 12/14/05, Steve [RubyTalk] <steve_rubytalk@shic.co.uk> wrote:
>
> Given that I only want to compute the offsets once, an obvious solution
> would be to construct an Array of String - each element representing a
> sub-string of the original... but this would double memory use. What
> would be the best way to avoid duplicating the character sequences and
> causing run-time bloat?

You don't need to create the substrings until you need them: Here's a
test which only allocates memory for the substrings as needed. As far
as I can tell, the substrings are released back to the GC as soon as
you are done with them.

class IndexedString < String
def initialize s, &indexer
super s
@offsets = yield self
self
end
def substring n
(0...(@offsets.size-1)) === n ?
self[@offsets[n]...@offsets[n+1]] :
nil
end
end

def build_index s
indexes = [0]
while (n=s.index('.',indexes.last+1)) do indexes << n+1 end
indexes << s.length+1
end

s = IndexedString.new( (("TESTINGTESTING!!"*1024)+".")*100) do |str|
build_index(str)
end #= +1600K
s.substring 10; #+16K
s.substring 95; #+16K


> By corollary, if I had a large number of Strings, what would be the most
> memory efficient way to represent their concatenation? If I had n mK
> stings, would I need another n*mK contiguous block of memory to
> represent their concatenation?

Not necessarily: You can build a class which takes an array of
strings and returns
"superstrings" only as needed. Here are my passing test cases, but
the class still needs a little work before I'll post it:

def test_MegaString
small_strings = %w( this is a collection of small strings)
mega = MegaString.new(small_strings)
assert_equal("t",mega[0])
assert_equal("this",mega[0,4])
assert_equal("hisisaco",mega[1,8])
assert_equal("ollect",mega[8,6])
assert_equal("a", mega[6...7])
assert_equal("col", mega[7..10])
assert_equal("lstrings", mega[23,-1])
assert_equal("lstrings", mega[23,230])
assert_equal("thisisacollectionofsmallstrings", mega.to_s)
end



-Adam


u2fan

12/26/2009 10:06:00 PM

0

Coming back from 1988 to now was a depressing experience, the panic
attacks, the demons, the insanity of now, all the dreams and hopes I
had back then killed by satan like a wife or job or security or
anything that can't be destroyed by the demons, totally alone and
helpless, a God that appears to be trillions of miles away and not
listening, not caring at all. Time has past, dreams destroyed,
emotions ripped away, mind destroyed and damaged beyond repair and
soul smashed. A prophet thrown in to the lions den ripped apart
mentally and in so many ways, if only the door to 1988 was open ?
Sadly not the only door open is death and heaven.

u2fan

12/26/2009 10:12:00 PM

0

On 26 Dec, 22:05, u2fan <u2fa...@googlemail.com> wrote:
> Coming back from 1988 to now was a depressing experience, the panic
> attacks, the demons, the insanity of now, all the dreams and hopes I
> had back then killed by satan like a wife or job or security or
> anything that can't be destroyed by the demons, totally alone and
> helpless, a God that appears to be trillions of miles away and not
> listening, not caring at all. Time has past, dreams destroyed,
> emotions ripped away, mind destroyed and damaged beyond repair and
> soul smashed. A prophet thrown in to the lions den ripped apart
> mentally and in so many ways, if only the door to 1988 was open ?
> Sadly not the only door open is death and heaven.

http://www.youtube.com/watch?v=-... Alive and Kicking

Satan is a savage beast, like a lion, ripping not just flesh, but
mind, soul, hope, will power, ego, esteem, friends, anything and most
of the time it seem there is no defence against the beast and no
license to kill it either, that is hell not being able to do anything
and being told by God to do nothing, just sit and wait to be eaten
alive by 666.

u2fan

12/26/2009 10:19:00 PM

0

On 26 Dec, 22:12, u2fan <u2fa...@googlemail.com> wrote:
> On 26 Dec, 22:05, u2fan <u2fa...@googlemail.com> wrote:
>
> > Coming back from 1988 to now was a depressing experience, the panic
> > attacks, the demons, the insanity of now, all the dreams and hopes I
> > had back then killed by satan like a wife or job or security or
> > anything that can't be destroyed by the demons, totally alone and
> > helpless, a God that appears to be trillions of miles away and not
> > listening, not caring at all. Time has past, dreams destroyed,
> > emotions ripped away, mind destroyed and damaged beyond repair and
> > soul smashed. A prophet thrown in to the lions den ripped apart
> > mentally and in so many ways, if only the door to 1988 was open ?
> > Sadly not the only door open is death and heaven.
>
> http://www.youtube.com/watch?v=-ag4XT... and Kicking
>
> Satan is a savage beast, like a lion, ripping not just flesh, but
> mind, soul, hope, will power, ego, esteem, friends, anything and most
> of the time it seem there is no defence against the beast and no
> license to kill it either, that is hell not being able to do anything
> and being told by God to do nothing, just sit and wait to be eaten
> alive by 666.

"You turn me on, you lift me up
And like the sweetest cup I'd share with you ( communion at mass )
You lift me up, don't you ever stop, I'm here with you
Now it's all or nothing
'Cause you say you'll follow through"

It is amazing Jesus' self control, he could have killed satan with the
sword in Matt 4 yet didn't, he could have nuked Rome with a single
thought in 33ad yet didn't, he allowed a savage beast to kill and
destroy billions of souls ripping them apart with sin and death and
demons and torture yet didn't, why the hell not ! And he let's the
beast reap more souls for hell than heaven ? It makes no sense ! Why
did God just let the beast kill his son at the cross ! Still the
tables will be turned when he comes back.