From: Caleb Clausen Date: 2007-06-11T16:15:08+09:00 Subject: Re: Array#inject to create a hash versus Hash[*array.collect{}.flatten] -- Speed, segfault That segfault shouldn't be happening... I guess you've found a bug. I modified your benchmark slightly (reduced id_list to 100_000 elements, since I don't want to wait more than an hour for it to finish) and I didn't get a segfault, but the results are definitely strange: Rehearsal -------------------------------------------- inject 0.320000 0.350000 0.670000 ( 0.682610) non-bang 7.700000 0.590000 8.290000 ( 8.522170) bang 7.150000 0.230000 7.380000 ( 7.392471) two-bang 6.500000 0.220000 6.720000 ( 6.766079) ---------------------------------- total: 23.060000sec user system total real inject 0.700000 0.310000 1.010000 ( 1.019340) non-bang 131.260000 1.440000 132.700000 (132.823762) bang 134.610000 1.280000 135.890000 (136.323224) two-bang 137.260000 0.750000 138.010000 (138.027025) Why is the rehearsal so much faster? Especially for the various collect implementations? (I'm running Debian Etch, Ruby 1.8.5.) I expect that the fastest way to do what you want is like this: id_list = [:test1, :test2, :test3] result={} id_list.each { |e| result[e]=e.object_id } Anyway, that's what I usually do. I have several times wanted a method on Enumerable that helps you transform it into a hash. Something like: module Enumerable def map_to_hash result={} each { |e| k,v=*yield(e) result[k]=v } return result end end id_list.map_to_hash {|x| [x,x.object_id]} I don't know what the performance of this might be.... It's creating a 2-element hash on each loop now, but maybe that has minimal cost.