Skip to content

Instantly share code, notes, and snippets.

@thomascharbonnel
Last active October 9, 2016 12:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomascharbonnel/f023ca137f2b2b7021cbe2d580485cd4 to your computer and use it in GitHub Desktop.
Save thomascharbonnel/f023ca137f2b2b7021cbe2d580485cd4 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'set'
require 'fds'
Benchmark.bm(30) do |bm|
bm.report('Using FDS::UnorderedSet') do
set = FDS::UnorderedSet.new
10_000.times do
set << rand
set.find_index(42)
end
end
bm.report('Using default Ruby Set') do
set = Set.new
10_000.times do
set << rand
set.find_index(42)
end
end
end
# user system total real
# Using FDS::UnorderedSet 0.010000 0.000000 0.010000 ( 0.006958)
# Using default Ruby Set 4.030000 0.010000 4.040000 ( 4.059034)
@rringler
Copy link

rringler commented Oct 6, 2016

Minor nit: but the execution time of puts "Test 0: #{end_test_0 - start}" is getting added to the second set's benchmark. Might be better to have have to separate start variables just to keep things apples to apples.

@thomascharbonnel
Copy link
Author

thomascharbonnel commented Oct 9, 2016

@rringler, true. I just updated the gist using recommended gem Benchmark. The result should be closer to reality. 😄
One should note that the slow operation for Ruby Set here is Enumerable#find_index. Insertion is about 15% slower on FDS implementation (because of hash values initialization).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment