From: Erik Veenstra Date: 2006-08-15T02:14:10+09:00 Subject: Re: hash.keys and hash.values > Here's another solution - maybe it's even more efficient as > it doesn't need the transposing: Well, a quick benchmark... No, it isn't more efficient. Neither time-wise, nor memory-wise. gegroet, Erik V. - http://www.erikveen.dds.nl/ ---------------------------------------------------------------- $ cat test.rb require "ev/ruby" GC.disable hash = {} 1000.times do |n| hash[n] = n*n end case ARGV.shift when "transpose" bm do 1000.times do keys, values = hash.to_a.transpose end end when "inject" bm do 1000.times do keys, values = hash.inject([[],[]]){|(ks,vs),(k,v)| [ks << k, vs << v]} end end else raise "uh?" end puts meminfo $ ruby test.rb transpose VmSize: 58284 kB CPU ELAPSED COUNT CPU/COUNT LABEL 1.020000 1.153397 1 1.020000 "test.rb:13" $ ruby test.rb inject VmSize: 146676 kB CPU ELAPSED COUNT CPU/COUNT LABEL 2.700000 2.826171 1 2.700000 "test.rb:19" ----------------------------------------------------------------