Skip to content

Instantly share code, notes, and snippets.

@epitron
Last active May 18, 2020 05:21
Show Gist options
  • Save epitron/1625d93d0b82c32e7395 to your computer and use it in GitHub Desktop.
Save epitron/1625d93d0b82c32e7395 to your computer and use it in GitHub Desktop.
Benchmark Ruby's various key/value databases (GDBM, SDBM, CDB)
require 'epitools'
%w[gdbm sdbm cdb].each { |lib| require lib }
DBS = [
[GDBM, GDBM],
[SDBM, SDBM],
[CDBMake, CDB ],
]
NUM = 100000
READLOOPS = 5
VALS = (0..NUM).map(&:to_s)
DBS.each do |createmod, openmod|
dbfile = Path["/tmp/test-#{openmod.name}"]
dbfile.rm if dbfile.exists?
puts "=== Benchmarking #{openmod} ==="
time "creating db and writing #{NUM} values" do
createmod.open(dbfile.to_s) do |db|
VALS.each { |val| db[val] = val }
end
end
openmod.open(dbfile.to_s) do |db|
time "reading #{NUM} values #{READLOOPS} times" do
READLOOPS.times do
VALS.each {|val| db[val] }
end
end
end
time "opening DB 5000 times and reading one value" do
5000.times { openmod.open(dbfile.to_s) { |d| d[VALS.first] } }
end
puts "DB size: #{Path["#{dbfile}*"].sum(&:size).commatize} bytes"
puts
end
=== Benchmarking GDBM ===
[creating db and writing 100000 values] elapsed time: 0.72189s
[reading 100000 values 5 times] elapsed time: 0.66752s
[opening DB 5000 times and reading one value] elapsed time: 1.29645s
DB size: 10,567,680 bytes
=== Benchmarking SDBM ===
[creating db and writing 100000 values] elapsed time: 0.56532s
[reading 100000 values 5 times] elapsed time: 1.29565s
[opening DB 5000 times and reading one value] elapsed time: 0.08798s
DB size: 2,101,248 bytes
=== Benchmarking CDB ===
[creating db and writing 100000 values] elapsed time: 0.10108s
[reading 100000 values 5 times] elapsed time: 0.17053s
[opening DB 5000 times and reading one value] elapsed time: 0.09612s
DB size: 3,379,864 bytes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment