Skip to content

Instantly share code, notes, and snippets.

@defHLT
Last active December 9, 2015 23:38
Show Gist options
  • Save defHLT/4345384 to your computer and use it in GitHub Desktop.
Save defHLT/4345384 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'set'
arr = []
set = Set.new
1000.times do |_|
r = rand(1000)
arr << r
set << r
end
Benchmark.bm(10) do |x|
check_values = (1..100_000).map { |_| rand(1000) }
x.report("array") do
check_values.each do |v|
arr.include? v
end
end
x.report("array_uniq") do
arr_uniq = arr.uniq
check_values.each do |v|
arr_uniq.include? v
end
end
x.report("set") do
check_values.each do |v|
set.include? v
end
end
end
#Output:
#ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
#user system total real
#array 4.650000 0.010000 4.660000 ( 4.661938)
#array_uniq 2.960000 0.000000 2.960000 ( 2.965344)
#set 0.040000 0.000000 0.040000 ( 0.034351)
#jruby 1.5.6 (ruby 1.8.7 patchlevel 249) (2012-12-11 6586) (OpenJDK 64-Bit Server VM 1.6.0_24) [amd64-java]
#user system total real
#array 3.033000 0.000000 3.033000 ( 2.977000)
#array_uniq 2.025000 0.000000 2.025000 ( 2.024000)
#set 0.218000 0.000000 0.218000 ( 0.218000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment