Created
April 12, 2013 06:41
-
-
Save jmdeldin/5369954 to your computer and use it in GitHub Desktop.
Benchmarking using semi-unnatural Array methods (#rotate and #drop) against the more natural Set difference.
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
Rehearsal ---------------------------------------------------------------- | |
array: rotating and dropping 0.050000 0.000000 0.050000 ( 0.055791) | |
set: difference 8.480000 0.020000 8.500000 ( 8.500697) | |
set: dup and delete 8.890000 0.020000 8.910000 ( 8.908871) | |
------------------------------------------------------ total: 17.460000sec | |
user system total real | |
array: rotating and dropping 0.060000 0.000000 0.060000 ( 0.056425) | |
set: difference 8.830000 0.010000 8.840000 ( 8.842558) | |
set: dup and delete 8.940000 0.030000 8.970000 ( 8.969234) | |
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' | |
require 'set' | |
LIMIT = 5_000 | |
WORDS = File.foreach('/usr/share/dict/words').lazy.map(&:chomp).take(LIMIT).to_a | |
Benchmark.bmbm do |x| | |
x.report("array: rotating and dropping") do | |
partitions = WORDS | |
partitions.each_with_index do |part, i| | |
others = partitions.rotate(i).drop(1) | |
end | |
end | |
x.report("set: difference") do | |
partitions = Set.new(WORDS) | |
partitions.each do |part| | |
others = partitions - Set.new([part]) | |
end | |
end | |
x.report("set: dup and delete") do | |
partitions = Set.new(WORDS) | |
partitions.each do |part| | |
others = partitions.dup.delete(part) | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment