Last active
December 21, 2015 15:10
-
-
Save qrilka/adc425e24b0eeeec2630 to your computer and use it in GitHub Desktop.
File backed "dicts"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ time python leveldb_test.py | |
real 0m0.153s | |
user 0m0.040s | |
sys 0m0.010s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import leveldb | |
key = '1234567789' | |
db = leveldb.LevelDB('./db') | |
for i in range(1, 50000): | |
try: | |
db.Get(key) | |
except KeyError: | |
db.Put(key, '') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ time python sqlite_raw_test.py | |
real 0m1.817s | |
user 0m1.000s | |
sys 0m0.810s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ time python sqlitedict_test.py | |
real 0m9.525s | |
user 0m9.150s | |
sys 0m3.550s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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