Hash vs. array (dictionary lookup)

Hi,

This code took 3 or 4 seconds to complete on my 900MHz PIII with 256Mb RAM.

h = {}
x = [“abd123”] * 90000
x.each_with_index do |y, i|
h[y + i.to_s] = 1
end
puts h.size

Very strange. I run my code, which is almost same as above again, it runs
very fast. But when I do this:

ruby -r profile loadtohash.rb dict.txt

It becomes very slow. I did the same thing (-r profile) with the loadtoarray
version, it worked just like without profiling…

What on earth is happening?

Regards,
Shannon

···

STOP MORE SPAM with the new MSN 8 and get 2 months FREE*
http://join.msn.com/?page=features/junkmail

Profiling intercepts the execution of each Ruby-level statement.
Consider the number of statements executed in your two chunks of code.

Dave

···

On Wed, 2002-12-18 at 13:39, Shannon Fang wrote:

Hi,

This code took 3 or 4 seconds to complete on my 900MHz PIII with 256Mb RAM.

h = {}
x = [“abd123”] * 90000
x.each_with_index do |y, i|
h[y + i.to_s] = 1
end
puts h.size

Very strange. I run my code, which is almost same as above again, it runs
very fast. But when I do this:

ruby -r profile loadtohash.rb dict.txt