Concatenating Hash's

What is the fastest way to concatenate two hash tables?

···

David King Landrith
(w) 617.227.4469x213
(h) 617.696.7133

Generic quotes offend nobody.
–Unknown

public key available upon request

“fastest” might be taken different ways; fastest runtime? Fastest to
code?

Anyway, check:
http://www.rubycentral.com/book/ref_c_hash.html#Hash.update

···

— David Landrith dlandrith@mac.com wrote:

What is the fastest way to concatenate two hash tables?


Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!

What is the fastest way to concatenate two hash tables?

Have you tried Hash#update ?

But you ask about the fastest way… hmm.

expand -t2 b.rb
class String
def String.rand(size=15)
s = String.new
size.times do |i|
s << (Kernel.rand(93)+33) # ascii range
end
s
end
end

class Hash
def Hash.rand(elements, width)
h = Hash.new
elements.times do
key = String.rand(width)
h[key] = Kernel.rand(899)+100
end
h
end
end

h1 = Hash.rand(100, 5)
h2 = Hash.rand(100, 5)
puts “h1=#{h1.size} h2=#{h2.size}”
h1.update(h2)
puts “h1=#{h1.size} h2=#{h2.size}”

ruby b.rb
h1=100 h2=100
h1=200 h2=100

Sorry no profiling… maybe someone else can add it ?

···

On Wed, 25 Jun 2003 01:32:27 +0900, David Landrith wrote:


Simon Strandgaard

Perfect. Thanks!

I spent the last 1/2 hour combing through pickaxe for exactly this, and
I can’t believe I missed it.

···

On Tuesday, June 24, 2003, at 11:34 AM, Michael Campbell wrote:

— David Landrith dlandrith@mac.com wrote:

What is the fastest way to concatenate two hash tables?

“fastest” might be taken different ways; fastest runtime? Fastest to
code?

Anyway, check:
http://www.rubycentral.com/book/ref_c_hash.html#Hash.update


Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com


David King Landrith
(w) 617.227.4469x213
(h) 617.696.7133

Generic quotes offend nobody.
–Unknown

public key available upon request

Simon Strandgaard wrote:

What is the fastest way to concatenate two hash tables?

Have you tried Hash#update ?

But you ask about the fastest way… hmm.

expand -t2 b.rb

class String
def String.rand(size=15)
s = String.new
size.times do |i|
s << (Kernel.rand(93)+33) # ascii range
end
s
end
end

class Hash
def Hash.rand(elements, width)
h = Hash.new
elements.times do
key = String.rand(width)
h[key] = Kernel.rand(899)+100
end
h
end
end

h1 = Hash.rand(100, 5)
h2 = Hash.rand(100, 5)
puts “h1=#{h1.size} h2=#{h2.size}”
h1.update(h2)
puts “h1=#{h1.size} h2=#{h2.size}”

ruby b.rb

h1=100 h2=100
h1=200 h2=100

Sorry no profiling… maybe someone else can add it ?

require ‘profile’

?

···

On Wed, 25 Jun 2003 01:32:27 +0900, David Landrith wrote:


Simon Strandgaard


dc -e
4ddod3dddn1-89danrn10-dan3+ann6dan2an13dn1+dn2-dn3+5ddan2/9+an13nap

I missed it too, actually. I only knew something that did what you
want existed since I remembered reading the “will override existing
values with the same key” piece; I went to the Hash page and did a
search for “over”. Somehow, I expected it to be Hash#+. “update”
doesn’t stick in my brain for some reason.

···

— David King Landrith dave@landrith.com wrote:

Perfect. Thanks!

I spent the last 1/2 hour combing through pickaxe for exactly this,
and I can’t believe I missed it.


Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!

“David King Landrith” dave@landrith.com schrieb im Newsbeitrag
news:436D22A6-A65A-11D7-924A-000393DC9B9C@landrith.com

Perfect. Thanks!

I spent the last 1/2 hour combing through pickaxe for exactly this, and
I can’t believe I missed it.

In fact, it’s a 1liner:

def hash_join(h1,h2); h1.dup.update(h2); end

you could also do

class Hash; def +(o); dup.update(o); end; end

h1={1,2,3,4}
h2={“a”,“b”}
h1 + h2 # => {“a”=>“b”, 1=>2, 3=>4}

:slight_smile:

robert

Yes I know ‘profile.rb’ … I wasn’t aware of Hash#update, so I started
to write a function for filling up a hash with random stuff.
Then I discovered Hash#update… and felt very ackward :slight_smile:

I don’t know how to benchmark hash concatenation ?

I assume Hash#update is the most optimal one can get.

···

On Mon, 30 Jun 2003 00:08:39 +0000, Anders Borch wrote:

Simon Strandgaard wrote:

Sorry no profiling… maybe someone else can add it ?

require ‘profile’

?


Simon Strandgaard

If your hash-tables has differents sizes, I suppose that it will be
fastest to merge the smallest into the biggest.

Just a thought.

···

On Wed, 25 Jun 2003 10:45:02 +0200, Robert Klemme wrote:

def hash_join(h1,h2); h1.dup.update(h2); end

you could also do

class Hash; def +(o); dup.update(o); end; end

h1={1,2,3,4}
h2={“a”,“b”}
h1 + h2 # => {“a”=>“b”, 1=>2, 3=>4}


Simon Strandgaard

“Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com schrieb im
Newsbeitrag news:pan.2003.06.25.08.56.32.933291@sneakemail.com

···

On Wed, 25 Jun 2003 10:45:02 +0200, Robert Klemme wrote:

def hash_join(h1,h2); h1.dup.update(h2); end

you could also do

class Hash; def +(o); dup.update(o); end; end

h1={1,2,3,4}
h2={“a”,“b”}
h1 + h2 # => {“a”=>“b”, 1=>2, 3=>4}

If your hash-tables has differents sizes, I suppose that it will be
fastest to merge the smallest into the biggest.

You mean

class Hash; def +(o); if size >= o.size then dup.update(o); else o+self;
end; end; end

?

robert

Hmm…

In Ruby-1.8.0 there is also: Hash#merge and Hash#merge!

How is Hash#update different from Hash#merge! are one just an aliases ?

···

On Wed, 25 Jun 2003 18:10:05 +0200, Robert Klemme wrote:

“Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com schrieb im
Newsbeitrag news:pan.2003.06.25.08.56.32.933291@sneakemail.com

On Wed, 25 Jun 2003 10:45:02 +0200, Robert Klemme wrote:

def hash_join(h1,h2); h1.dup.update(h2); end

you could also do

class Hash; def +(o); dup.update(o); end; end

h1={1,2,3,4}
h2={“a”,“b”}
h1 + h2 # => {“a”=>“b”, 1=>2, 3=>4}

If your hash-tables has differents sizes, I suppose that it will be
fastest to merge the smallest into the biggest.

You mean

class Hash; def +(o); if size >= o.size then dup.update(o); else o+self;
end; end; end

?


Simon Strandgaard

Hi,

···

In message “Re: Concatenating Hash’s” on 03/07/02, “Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com writes:

How is Hash#update different from Hash#merge! are one just an aliases ?

Aliases.

						matz.