Skip to content

Instantly share code, notes, and snippets.

@qrilka
Last active December 21, 2015 15:10
Show Gist options
  • Save qrilka/adc425e24b0eeeec2630 to your computer and use it in GitHub Desktop.
Save qrilka/adc425e24b0eeeec2630 to your computer and use it in GitHub Desktop.
File backed "dicts"
$ time python leveldb_test.py
real 0m0.153s
user 0m0.040s
sys 0m0.010s
import leveldb
key = '1234567789'
db = leveldb.LevelDB('./db')
for i in range(1, 50000):
try:
db.Get(key)
except KeyError:
db.Put(key, '')
$ time python sqlite_raw_test.py
real 0m1.817s
user 0m1.000s
sys 0m0.810s
import sqlite3
key = '1234567789'
conn = sqlite3.connect('/tmp/sqlite.db', isolation_level=None, check_same_thread=False)
conn.execute('PRAGMA journal_mode = OFF')
MAKE_TABLE = 'CREATE TABLE IF NOT EXISTS t (key TEXT PRIMARY KEY)'
conn.execute(MAKE_TABLE)
conn.commit()
cur = conn.cursor()
for i in range(1, 50000):
HAS_ITEM = 'SELECT 1 FROM t WHERE key = ?'
conn.execute(HAS_ITEM, (key,))
selected = cur.fetchone()
if selected is None:
ADD_ITEM = 'REPLACE INTO t (key) VALUES (?)'
conn.execute(ADD_ITEM, (key,))
$ time python sqlitedict_test.py
real 0m9.525s
user 0m9.150s
sys 0m3.550s
from sqlitedict import SqliteDict
d = SqliteDict("/tmp/sqlite.db", flag='n', autocommit = True)
key = '1234567789'
for i in range(1, 50000):
if key not in d:
d['key'] = ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment