Skip to content

Instantly share code, notes, and snippets.

@rust
Created January 9, 2009 03:00
Show Gist options
  • Save rust/45005 to your computer and use it in GitHub Desktop.
Save rust/45005 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'tokyocabinet'
require 'digest/sha1'
require 'pp'
bdb = TokyoCabinet::BDB.new
unless bdb.open("dbd-benchmark.dbd", TokyoCabinet::BDB::OWRITER | TokyoCabinet::BDB::OCREAT)
exit
end
class Hoge
attr_accessor :id, :name, :age, :sex, :text
def initialize(num = rand(1000))
@id = num
@name = "#{@id}さん"
@age = num.to_s(36)
@sex = num % 2
@text = @name
end
def ==(hoge)
@id == hoge.id and @name == hoge.name and @age == hoge.age and @sex == hoge.sex
end
end
puts Benchmark::CAPTION
puts Benchmark.measure {
(1..100_000).each do |i|
hash = Digest::SHA1.hexdigest(i.to_s)
bdb.put(hash, Marshal.dump(Hoge.new(i)))
end
}
count = 0
puts Benchmark::CAPTION
puts Benchmark.measure {
(1..100_000).each do |i|
hash = Digest::SHA1.hexdigest(i.to_s)
count += 1 if Marshal.load(bdb.get(hash)) == Hoge.new(i)
end
}
count # => 100000
a = Hoge.new(1)
b = Hoge.new(1)
a == b # => true
# >> user system total real
# >> 2.630000 0.200000 2.830000 ( 2.833207)
# >> user system total real
# >> 3.330000 0.140000 3.470000 ( 4.034548)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment