Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Does String cache its 'hash' return value?

Reply
Thread Tools

Does String cache its 'hash' return value?

 
 
Asfand Yar Qazi
Guest
Posts: n/a
 
      12-23-2003
What I mean is, if I repeatedly call a String's hash method (i.e. like
using it regularly to access a Hash) will the hash value get
recalculated every time?






--
http://www.it-is-truth.org/

 
Reply With Quote
 
 
 
 
Florian Frank
Guest
Posts: n/a
 
      12-23-2003
On Tue, 2003-12-23 at 01:56, Asfand Yar Qazi wrote:
> What I mean is, if I repeatedly call a String's hash method (i.e. like
> using it regularly to access a Hash) will the hash value get
> recalculated every time?


Yes, it will. Have you considered unsing Symbols instead of Strings?
The hash value "calculation" of a Symbol is O(1).

--
o=lambda{|o|p o};O=Struct.new(:a,:b,:c);e=%q(_(?h,_(?h,_(?\ ,_(?s,_(?u,_(74)),
_(?t)),_(?t,_(?o,_(?n,_(?a))))),_(82,_(?r,_(?e),_( 32)),_(32,_(98,_(?u),_(?y)))
)),_(?r,_(99,_(97),_(?k,nil,_(?e))),_(10))));def _(*a)O.new(*a)end;class O;def
e(&o)b&&b.e(&o);o[a];c&&c.e(&o)end;end;def p(o)print(''<<o)end;eval(e).e(&o)



 
Reply With Quote
 
 
 
 
Asfand Yar Qazi
Guest
Posts: n/a
 
      12-24-2003
Florian Frank wrote:
> On Tue, 2003-12-23 at 01:56, Asfand Yar Qazi wrote:
>
>>What I mean is, if I repeatedly call a String's hash method (i.e. like
>>using it regularly to access a Hash) will the hash value get
>>recalculated every time?

>
>
> Yes, it will. Have you considered unsing Symbols instead of Strings?
> The hash value "calculation" of a Symbol is O(1).
>


Depends... if Symbols are used as the keys for a hash table, can they be
as easily accessed from extension modules as if Strings were used?







--
http://www.it-is-truth.org/

 
Reply With Quote
 
gabriele renzi
Guest
Posts: n/a
 
      12-24-2003
il Wed, 24 Dec 2003 00:11:27 +0000, Asfand Yar Qazi
<im_not_giving_it_here@i_hate_spam.com> ha scritto::


>>
>> Yes, it will. Have you considered unsing Symbols instead of Strings?
>> The hash value "calculation" of a Symbol is O(1).
>>

>
>Depends... if Symbols are used as the keys for a hash table, can they be
>as easily accessed from extension modules as if Strings were used?


why the should'nt ? Many lybraries make use of symbols as constants,
ruby-gtk or Pdf::Writer are examples.. and, btw "everything is an
object" make everything work as everything else
 
Reply With Quote
 
Florian Frank
Guest
Posts: n/a
 
      12-24-2003
On Wed, 2003-12-24 at 01:06, Asfand Yar Qazi wrote:
> Depends... if Symbols are used as the keys for a hash table, can they be
> as easily accessed from extension modules as if Strings were used?


You can use ID2SYM(rb_intern("foo")) to get the VALUE of symbol :foo. I
don't think it's too complicated.

--
o=lambda{|o|p o};O=Struct.new(:a,:b,:c);e=%q(_(?h,_(?h,_(?\ ,_(?s,_(?u,_(74)),
_(?t)),_(?t,_(?o,_(?n,_(?a))))),_(82,_(?r,_(?e),_( 32)),_(32,_(98,_(?u),_(?y)))
)),_(?r,_(99,_(97),_(?k,nil,_(?e))),_(10))));def _(*a)O.new(*a)end;class O;def
e(&o)b&&b.e(&o);o[a];c&&c.e(&o)end;end;def p(o)print(''<<o)end;eval(e).e(&o)


 
Reply With Quote
 
nobu.nokada@softhome.net
Guest
Posts: n/a
 
      12-24-2003
Hi,

At Tue, 23 Dec 2003 09:56:53 +0900,
Asfand Yar Qazi wrote:
> What I mean is, if I repeatedly call a String's hash method (i.e. like
> using it regularly to access a Hash) will the hash value get
> recalculated every time?


Hash (accurately, st library) has cache, but String doesn't.

--
Nobu Nakada


 
Reply With Quote
 
Asfand Yar Qazi
Guest
Posts: n/a
 
      12-24-2003
Florian Frank wrote:
> On Wed, 2003-12-24 at 01:06, Asfand Yar Qazi wrote:
>
>>Depends... if Symbols are used as the keys for a hash table, can they be
>>as easily accessed from extension modules as if Strings were used?

>
>
> You can use ID2SYM(rb_intern("foo")) to get the VALUE of symbol :foo. I
> don't think it's too complicated.
>


WOW DAT WAS KWICK U R LEE7!!!11111





--
http://www.it-is-truth.org/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 14 04-03-2010 10:08 AM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 0 04-01-2010 10:25 PM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
Determining method type given its string name presentation and its corresponding object reference. Apple Python 3 08-01-2005 03:16 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57