Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 17th, 2012  |  syntax: None  |  size: 0.58 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. require 'benchmark'
  2. require 'set'
  3.  
  4. a = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
  5. h = {'a' => 1, 'b' => 1, 'c' => 1, 'd' => 1,
  6.      'e' => 1, 'f' => 1, 'g' => 1}
  7.  
  8. s = Set.new(a)
  9.  
  10. Benchmark.bm do |x|
  11.   x.report do
  12.     1_000_000.times do
  13.       a.include?('d')
  14.     end
  15.   end
  16.   x.report do
  17.     1_000_000.times do
  18.       h['d']
  19.     end
  20.   end
  21.   x.report do
  22.     1_000_000.times do
  23.       s.include?('d')
  24.     end
  25.   end
  26.  
  27.   #    user     system      total        real
  28.   # 0.520000   0.000000   0.520000 (  0.521729)
  29.   # 0.260000   0.000000   0.260000 (  0.258423)
  30.   # 0.350000   0.000000   0.350000 (  0.352357)
  31. end