Skip to content

Instantly share code, notes, and snippets.

@igrigorik
Created February 14, 2009 15:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save igrigorik/64391 to your computer and use it in GitHub Desktop.
Save igrigorik/64391 to your computer and use it in GitHub Desktop.
require "rubygems"
require "tokyocabinet"
require "benchmark"
include TokyoCabinet
records = 1000000
hdb = HDB::new # Hash database; acts as a key value store
hdb.open("casket.hdb", HDB::OWRITER | HDB::OCREAT)
bdb = BDB::new # B-Tree database; keys may have multiple values
bdb.open("casket.bdb", BDB::OWRITER | BDB::OCREAT)
[hdb, bdb].each do |db|
print "#{db.class} Insert", Benchmark.measure { records.times { |r| db.put("key-#{r}", r) }}
print "#{db.class} Read", Benchmark.measure { records.times {|r| db.get("key-#{r}") }}
print "#{db.class} Iterate", Benchmark.measure {
if db.is_a?(HDB)
db.iterinit
while key = db.iternext
db.get(key)
end
elsif db.is_a?(BDB)
cur = BDBCUR::new(db)
cur.first
while key = cur.key
cur.val
cur.next
end
end
}
db.close
puts
end
fdb = FDB::new # Fixed-length database; in-memory hash table
fdb.open("casket.fdb", FDB::OWRITER | FDB::OCREAT)
print "#{fdb.class} Insert", Benchmark.measure { records.times { |r| fdb.put(r, r.to_s) }}
print "#{fdb.class} Read", Benchmark.measure { records.times {|r| fdb.get(r) }}
print "#{fdb.class} Iterate", Benchmark.measure {
fdb.iterinit
while key = fdb.iternext
fdb.get(key)
end
}
fdb.close
puts
tdb = TDB::new # Table database: column and query support
tdb.open("casket.tdb", TDB::OWRITER | TDB::OCREAT)
print "#{tdb.class} Insert", Benchmark.measure { records.times {|r| tdb.put(r, {"key" => r, "col" => "test"}) }}
print "#{tdb.class} Read", Benchmark.measure { records.times {|r| tdb.get(r) }}
print "#{tdb.class} Iterate", Benchmark.measure { tdb.each {|k,v| } }
print "#{tdb.class} Index iterate", Benchmark.measure {
qry = TDBQRY::new(tdb)
qry.addcond("col", TDBQRY::QCSTROR, "test")
res = qry.search
res.each { |rkey| tdb.get(rkey) }
}
tdb.close
puts
# TokyoCabinet::HDB Insert 0.970000 2.220000 3.190000 ( 3.660773)
# TokyoCabinet::HDB Read 1.990000 0.040000 2.030000 ( 2.175728)
# TokyoCabinet::HDB Iterate 1.810000 0.030000 1.840000 ( 2.003771)
#
# TokyoCabinet::BDB Insert 3.700000 0.450000 4.150000 ( 4.882034)
# TokyoCabinet::BDB Read 2.970000 0.190000 3.160000 ( 3.492481)
# TokyoCabinet::BDB Iterate 2.660000 0.120000 2.780000 ( 3.413598)
#
# TokyoCabinet::FDB Insert 0.610000 4.090000 4.700000 ( 7.125238)
# TokyoCabinet::FDB Read 1.570000 0.040000 1.610000 ( 2.260386)
# TokyoCabinet::FDB Iterate 1.850000 0.060000 1.910000 ( 2.380107)
#
# TokyoCabinet::TDB Insert 4.080000 4.400000 8.480000 ( 10.017332)
# TokyoCabinet::TDB Read 4.500000 0.110000 4.610000 ( 5.038626)
# TokyoCabinet::TDB Iterate 4.410000 0.090000 4.500000 ( 4.803168)
# TokyoCabinet::TDB Index iterate 4.160000 1.020000 5.180000 ( 5.487797)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment