Created
May 26, 2015 14:37
-
-
Save fcheung/316ae7ac30cd240bd834 to your computer and use it in GitHub Desktop.
set vs hash
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 'benchmark/ips' | |
| require 'set' | |
| set = Set[:$or, :$and, :$nor] | |
| array = %i($or $and $nor) | |
| Benchmark.ips do |x| | |
| x.config(:time => 10, :warmup => 2) | |
| x.report('set') do |times| | |
| i = 0 | |
| while i < times | |
| set.include? :$nor | |
| set.include? :$other | |
| i+= 1 | |
| end | |
| end | |
| x.report('array') do |times| | |
| i = 0 | |
| while i < times | |
| array.include? :$nor | |
| array.include? :$other | |
| i+= 1 | |
| end | |
| end | |
| x.compare! | |
| end |
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
| Calculating ------------------------------------- | |
| set 130.162k i/100ms | |
| array 133.122k i/100ms | |
| ------------------------------------------------- | |
| set 7.128M (± 5.3%) i/s - 71.068M | |
| array 7.891M (± 5.0%) i/s - 78.675M | |
| Comparison: | |
| array: 7890818.8 i/s | |
| set: 7128395.4 i/s - 1.11x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment