-
-
Save oriolgual/879401 to your computer and use it in GitHub Desktop.
Use with plain Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'benchmark' | |
include Benchmark | |
class Array | |
def to_hash_using_inject_push( use_array ) | |
Hash[*(0 .. self.size).inject( [] ) { |r,i| r.push( self[i], use_array[i] ) }] | |
end | |
def to_hash_using_zip( use_array ) | |
Hash[self.zip( use_array )] | |
end | |
def to_hash_using_zip_flatten( use_array ) | |
Hash[*self.zip( use_array ).flatten] | |
end | |
def to_hash_using_each_with_index( use_array ) | |
h = Hash.new | |
self.each_with_index { |k,i| h[k] = use_array[i] } | |
h | |
end | |
def to_hash_using_brute_force( use_array ) | |
h = Hash.new | |
( 0 ... self.size ).each { |i| h[self[i]] = use_array[i] } | |
h | |
end | |
end | |
class BenchmarkArrayToHash | |
def build_test_arrays( use_size ) | |
@a1 = [*0..use_size] | |
@a2 = [*0..use_size] | |
end | |
def initialize | |
limits = '10,50,100,1000,1300,1400,100000' | |
limits.split( ',' ).each do |val| | |
build_test_arrays( val.to_i ) | |
puts val | |
Benchmark.bm do |b| | |
b.report( 'brute force' ) { @a1.to_hash_using_brute_force( @a2 ) } | |
b.report( 'zip' ) { @a1.to_hash_using_zip( @a2 ) } | |
b.report( 'each with index' ) { @a1.to_hash_using_each_with_index( @a2 ) } | |
b.report( 'inject/push' ) { @a1.to_hash_using_inject_push( @a2 ) } | |
b.report( 'zip/flatten' ) { @a1.to_hash_using_zip_flatten( @a2 ) } | |
end | |
puts "\n\n" | |
end | |
end | |
end | |
BenchmarkArrayToHash.new |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
irb(main):007:0> a1 = [ :a, :b, :c ] | |
=> [:a, :b, :c] | |
irb(main):008:0> a2 = [ 1, 2, 3, ] | |
=> [1, 2, 3] | |
... | |
=> {:b=>2, :a=>1, :c=>3} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rb(main):003:0> h = Hash.new | |
=> {} | |
irb(main):005:0> ( 0 .. 2 ).each { |i| h[a1[i]] = a2[i] } | |
=> [:a, :b, :c] | |
irb(main):006:0> h | |
=> {:b=>2, :a=>1, :c=>3} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
irb(main):003:0> h = Hash.new | |
=> {} | |
irb(main):005:0> a1.each_with_index { |key,idx| h[key] = a2[idx] } | |
=> [:a, :b, :c] | |
irb(main):006:0> h | |
=> {:b=>2, :a=>1, :c=>3} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
irb(main):012:0> Hash[ :a, 1, :b, 2, :c, 3 ] | |
=> {:b=>2, :a=>1, :c=>3} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
irb(main):014:0> a1.zip( a2 ).flatten | |
=> [:a, 1, :b, 2, :c, 3] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
irb(main):018:0> Hash[*a1.zip(a2).flatten] | |
=> {:b=>2, :a=>1, :c=>3} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
irb(main):019:0> Hash[*(0..2).inject( [] ) { |r,i| r.push( a1[i], a2[i] ) }] | |
=> {:b=>2, :a=>1, :c=>3} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
irb(main):013:0> a1.zip( a2 ) | |
=> [[:a, 1], [:b, 2], [:c, 3]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment