Skip to content

Instantly share code, notes, and snippets.

@bricker
Created July 24, 2015 00:42
Show Gist options
  • Save bricker/6d97e1e591bbcbf07092 to your computer and use it in GitHub Desktop.
Save bricker/6d97e1e591bbcbf07092 to your computer and use it in GitHub Desktop.
ids = [1,3,5]; s=[OpenStruct.new(id: 1), OpenStruct.new(id: 2), OpenStruct.new(id: 3), OpenStruct.new(id: 4), OpenStruct.new(id: 5)]
irb(main):019:0> puts Benchmark.measure { 1_000_000.times { Hash[s.map { |o| [o.id, o] }].values_at(*ids) } }
2.390000 0.000000 2.390000 ( 2.394635)
=> nil
irb(main):020:0> puts Benchmark.measure { 1_000_000.times { Hash[s.map { |o| [o.id, o] }].values_at(*ids) } }
2.360000 0.000000 2.360000 ( 2.367024)
=> nil
irb(main):021:0> puts Benchmark.measure { 1_000_000.times { Hash[s.map { |o| [o.id, o] }].values_at(*ids) } }
2.380000 0.000000 2.380000 ( 2.385241)
=> nil
irb(main):022:0> puts Benchmark.measure { 1_000_000.times { s.select { |e| ids.include?(e.id) } } }
1.430000 0.000000 1.430000 ( 1.423978)
=> nil
irb(main):023:0> puts Benchmark.measure { 1_000_000.times { s.select { |e| ids.include?(e.id) } } }
1.440000 0.000000 1.440000 ( 1.448103)
=> nil
irb(main):024:0> puts Benchmark.measure { 1_000_000.times { s.select { |e| ids.include?(e.id) } } }
1.550000 0.000000 1.550000 ( 1.548810)
=> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment