Guest User

Untitled

a guest
Oct 21st, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. require "rubygems"
  2. require "rbench"
  3. require "set"
  4.  
  5. RANGE = (1..1000)
  6. ARRAY = RANGE.to_a
  7. SET = Set.new(ARRAY)
  8. HASH = ARRAY.inject({}) {|m, x| m[x] = true; m}
  9.  
  10. WORST_CASE_COMPLEXITY = ARRAY.size + 1
  11.  
  12. RBench.run(50_000) do
  13. column :one, :title => 'Array'
  14. column :two, :title => 'Set'
  15. column :three, :title => 'Range'
  16. column :four, :title => 'Hash'
  17.  
  18. report "Which is fastest for misses?" do
  19. one { ARRAY.include?(WORST_CASE_COMPLEXITY) }
  20. two { SET.include?(WORST_CASE_COMPLEXITY) }
  21. three { RANGE.include?(WORST_CASE_COMPLEXITY) }
  22. four { HASH.include?(WORST_CASE_COMPLEXITY) }
  23. end
  24.  
  25. report "Which is fastest for hits?" do
  26. one { ARRAY.include?(1) }
  27. two { SET.include?(1) }
  28. three { RANGE.include?(1) }
  29. four { HASH.include?(1) }
  30. end
  31. end
  32.  
  33. __END__
  34. Array | Set | Range | Hash |
  35. ----------------------------------------------------------------------------
  36. Which is fastest for misses? 2.237 | 0.016 | 0.016 | 0.009 |
  37. Which is fastest for hits? 0.007 | 0.015 | 0.017 | 0.008 |
Add Comment
Please, Sign In to add comment