#!/usr/bin/python import dumbdbm, gdbm, os, shutil, time import fsdbm def dbmbench(impl): words = [line.strip() for line in open('/usr/share/dict/american-english')] dirname = 'dbmbench.dbs' dbname = os.path.join(dirname, 'db') assert not os.path.exists(dirname) os.mkdir(dirname) start = time.time() db = impl.open(dbname, 'n') for word in words: db[word] = '1' db = impl.open(dbname, 'r') for word in words: assert word in db assert word + 'uaiowoaj' not in db duration = time.time() - start del db os.spawnl(os.P_WAIT, '/bin/ls', 'ls', '-l', dirname) os.spawnl(os.P_WAIT, '/usr/bin/du', 'du', '-sh', dirname) shutil.rmtree(dirname) return len(words), duration def print_dbmbench(name, impl): n, slow = dbmbench(impl) print "%s took %.2g seconds to handle %d keys." % (name, slow, n) def main(): print_dbmbench('dumbdbm', dumbdbm) print_dbmbench('gdbm', gdbm) print_dbmbench('fsdbm', fsdbm) if __name__ == '__main__': main()