Sha256: 76388aae8ce2b62aeddab0e0b0d950ae068a598040699a55cec9069021eaca9a

Contents?: true

Size: 1.86 KB

Versions: 43

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true
require 'benchmark'
require 'dbm'

MARSHAL_FILE = "marshal_test.db"
DBM_FILE = "dbm_test"
WRITE_TIMES = 1
READ_TIMES = 100
NUM_INDICES = 10_000
INDICES = ['33', '857', '5022', '8555']

def generate_index
  '0' * (rand * 4096).floor
end

def write_dbm
  File.unlink(DBM_FILE + ".db") if File.exist?(DBM_FILE + ".db")
  handle = DBM.new(DBM_FILE)
  NUM_INDICES.times {|t| handle[t.to_s] = Marshal.dump(generate_index) }
  handle.close
end

def read_dbm
  db = DBM.open(DBM_FILE)
  INDICES.each {|index| Marshal.load(db[index]) }
  db.close
end

def write_marshal
  File.unlink(MARSHAL_FILE) if File.exist?(MARSHAL_FILE)
  handle = {}
  NUM_INDICES.times {|t| handle[t.to_s] = generate_index }
  File.open(MARSHAL_FILE, "wb") {|f| f.write(Marshal.dump(handle)) }
end

def read_marshal
  db = Marshal.load(File.read(MARSHAL_FILE))
  INDICES.each {|index| db[index] }
end

Benchmark.bmbm do |x|
  x.report("marshal-write") { WRITE_TIMES.times { write_marshal } }
  x.report("dbm-write") { WRITE_TIMES.times { write_dbm } }
  x.report("marshal-read ") { READ_TIMES.times { read_marshal } }
  x.report("dbm-read ") { READ_TIMES.times { read_dbm } }
end

File.unlink(MARSHAL_FILE)
File.unlink(DBM_FILE + ".db")

__END__

Rehearsal -------------------------------------------------
marshal-write   0.090000   0.070000   0.160000 (  0.465820)
dbm-write       0.560000   0.570000   1.130000 (  3.045556)
marshal-read    4.640000   3.180000   7.820000 (  7.821978)
dbm-read        0.020000   0.020000   0.040000 (  0.070920)
---------------------------------------- total: 9.150000sec

                    user     system      total        real
marshal-write   0.080000   0.050000   0.130000 (  0.436561)
dbm-write       0.560000   0.550000   1.110000 (  2.030530)
marshal-read    4.670000   3.180000   7.850000 (  7.842232)
dbm-read        0.010000   0.020000   0.030000 (  0.053928)

Version data entries

43 entries across 42 versions & 7 rubygems

Version Path
qiita_org-0.1.25 gems/ruby/2.7.0/gems/yard-0.9.25/benchmarks/marshal_vs_dbm.rb
yard-0.9.25 benchmarks/marshal_vs_dbm.rb
yard-0.9.24 benchmarks/marshal_vs_dbm.rb
yard-0.9.23 benchmarks/marshal_vs_dbm.rb
yard-0.9.22 benchmarks/marshal_vs_dbm.rb
yard-0.9.21 benchmarks/marshal_vs_dbm.rb
yard-0.9.20 benchmarks/marshal_vs_dbm.rb
yard-0.9.19 benchmarks/marshal_vs_dbm.rb
yard-0.9.16 benchmarks/marshal_vs_dbm.rb
yard-0.9.15 benchmarks/marshal_vs_dbm.rb
yard-0.9.14 benchmarks/marshal_vs_dbm.rb
yard-0.9.13 benchmarks/marshal_vs_dbm.rb
yard-0.9.12 benchmarks/marshal_vs_dbm.rb
yard-0.9.11 benchmarks/marshal_vs_dbm.rb
yard-0.9.10 benchmarks/marshal_vs_dbm.rb
yard-0.9.9 benchmarks/marshal_vs_dbm.rb
etude_for_ruby-0.1.4 vendor/bundle/ruby/2.2.0/gems/yard-0.9.8/benchmarks/marshal_vs_dbm.rb
etude_for_ruby-0.1.4 vendor/bundle/ruby/2.4.0/gems/yard-0.9.8/benchmarks/marshal_vs_dbm.rb
mdg-1.0.1 vendor/bundle/ruby/2.3.0/gems/yard-0.9.8/benchmarks/marshal_vs_dbm.rb
yard-0.9.8 benchmarks/marshal_vs_dbm.rb