Skip to content

Instantly share code, notes, and snippets.

@Papierkorb
Last active November 19, 2015 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Papierkorb/3a00c4720d7eaa49efdf to your computer and use it in GitHub Desktop.
Save Papierkorb/3a00c4720d7eaa49efdf to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'set'
#require 'rbtree'
range = (1..10_000)
array = range.to_a
set = SortedSet.new array
n = 1..1_000
puts "#{RUBY_ENGINE} #{RUBY_VERSION} #{RUBY_PLATFORM}"
Benchmark.bm(9) do |x|
x.report("clone ary:"){ for i in n do; array.clone; end }
x.report("clone set:"){ for i in n do; set.clone; end }
x.report("array:"){ for i in n do; array.clone.delete rand range; end }
x.report("set:"){ for i in n do; set.clone.delete rand range; end }
end
ruby 2.2.3 x86_64-linux
user system total real
clone ary: 0.000000 0.000000 0.000000 ( 0.000217)
clone set: 2.280000 0.020000 2.300000 ( 2.307867)
array: 0.280000 0.000000 0.280000 ( 0.280849) # 0.280849 - 0.000217 = ~0.280632
set: 2.250000 0.000000 2.250000 ( 2.256817) # 2.307867 - 2.256817 = ~0.05105
jruby 2.2.2 java
user system total real
clone ary: 0.020000 0.000000 0.020000 ( 0.002725)
clone set: 0.570000 0.040000 0.610000 ( 0.347833)
array: 0.200000 0.000000 0.200000 ( 0.082413)
set: 0.500000 0.010000 0.510000 ( 0.312068)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment