Skip to content

Instantly share code, notes, and snippets.

@pjambet
Last active September 30, 2020 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjambet/0f4953fb9c379aab36de2bc8d65c51b1 to your computer and use it in GitHub Desktop.
Save pjambet/0f4953fb9c379aab36de2bc8d65c51b1 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'securerandom'
require_relative './dict'
RANDOM_BYTES = SecureRandom.bytes(16)
def setup_dict(size)
dict = BYORedis::Dict.new
size.times { |i| dict[i.to_s] = i.to_s }
dict
end
def dict_exp(dict, n, size)
n.times do
r = rand size
dict[r.to_s]
end
end
def setup_arr(size)
arr = []
size.times { |i| arr << [ i.to_s, i.to_s ] }
arr
end
def array_exp(arr, n, size)
n.times do
r = rand size
arr.each { |a| break if a[0] == r.to_s }
end
end
Benchmark.bm do |x|
n = 1000
size = 256
dict = setup_dict(size)
arr = setup_arr(size)
x.report { dict_exp(dict, n, size) }
x.report { array_exp(arr, n, size) }
end
~/dev/redis-in-ruby/code/chapter-7 chapter-7* ⇡
❯ ruby benchmark.rb
user system total real
0.021310 0.000308 0.021618 ( 0.022059)
0.022467 0.000290 0.022757 ( 0.023007)
~/dev/redis-in-ruby/code/chapter-7 chapter-7* ⇡
❯ ruby benchmark.rb
user system total real
0.022083 0.000222 0.022305 ( 0.022594)
0.022991 0.000163 0.023154 ( 0.023313)
~/dev/redis-in-ruby/code/chapter-7 chapter-7* ⇡
❯ ruby benchmark.rb
user system total real
0.021011 0.000173 0.021184 ( 0.021383)
0.023750 0.000293 0.024043 ( 0.024316)
~/dev/redis-in-ruby/code/chapter-7 chapter-7* ⇡
❯ ruby benchmark.rb
user system total real
0.021394 0.000167 0.021561 ( 0.021772)
0.021975 0.000188 0.022163 ( 0.022321)
~/dev/redis-in-ruby/code/chapter-7 chapter-7* ⇡
❯ ruby benchmark.rb
user system total real
0.020863 0.000116 0.020979 ( 0.021092)
0.021790 0.000196 0.021986 ( 0.022155)
~/dev/redis-in-ruby/code/chapter-7 chapter-7* ⇡
❯ ruby benchmark.rb
user system total real
0.021434 0.000139 0.021573 ( 0.021746)
0.023045 0.000238 0.023283 ( 0.023473)
~/dev/redis-in-ruby/code/chapter-7 chapter-7* ⇡
❯ ruby benchmark.rb
user system total real
0.019905 0.000096 0.020001 ( 0.020089)
0.024888 0.000241 0.025129 ( 0.025634)
~/dev/redis-in-ruby/code/chapter-7 chapter-7* ⇡
❯ ruby benchmark.rb
user system total real
0.023226 0.000229 0.023455 ( 0.024171)
0.028914 0.000278 0.029192 ( 0.029885)
~/dev/redis-in-ruby/code/chapter-7 chapter-7* ⇡
❯ ruby benchmark.rb
user system total real
0.027375 0.000214 0.027589 ( 0.028001)
0.022193 0.000206 0.022399 ( 0.022571)
~/dev/redis-in-ruby/code/chapter-7 chapter-7* ⇡
❯ ruby benchmark.rb
user system total real
0.020572 0.000100 0.020672 ( 0.020837)
0.024418 0.000204 0.024622 ( 0.024827)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment