Created
October 27, 2014 10:44
-
-
Save alexpearce/e580b174e55d02a57388 to your computer and use it in GitHub Desktop.
Timing operations on an SQLite database.
This file contains 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 timeit | |
import os | |
DB_PATH = 'database.db' | |
# Number of runs to generate | |
NRUNS = int(1e6) | |
insert_setup = """import sqlite3 | |
con = sqlite3.connect('{0}') | |
con.execute('CREATE TABLE runs (run INTEGER PRIMARY KEY)') | |
""".format(DB_PATH) | |
# Explicit insertation loop | |
insert_stmt = "for i in range(int({0})): con.execute('INSERT INTO runs (run) VALUES (?)', (i,))".format(NRUNS) | |
# Implicit insertation loop | |
insert_bulk_stmt = "runs = range(int({0})); con.executemany('INSERT INTO runs (run) VALUES (?)', [(i,) for i in runs])".format(NRUNS) | |
insert_time = timeit.timeit(insert_stmt, insert_setup, number=1) | |
os.remove(DB_PATH) | |
insert_bulk_time = timeit.timeit(insert_stmt, insert_setup, number=1) | |
os.remove(DB_PATH) | |
delete_setup = """import sqlite3 | |
con = sqlite3.connect('{0}') | |
con.execute('CREATE TABLE runs (run INTEGER PRIMARY KEY)') | |
for i in range(int({1})): | |
con.execute('INSERT INTO runs (run) VALUES (?)', (i,)) | |
""".format(DB_PATH, NRUNS) | |
# Explicit deletion loop | |
delete_stmt = "for i in range(int({0})): con.execute('DELETE FROM runs WHERE run=?', (i,))".format(NRUNS) | |
# Implicit deletion loop | |
delete_bulk_stmt = "runs = range(int({0})); con.executemany('DELETE FROM runs WHERE run=?', [(i,) for i in runs])".format(NRUNS) | |
delete_time = timeit.timeit(delete_stmt, delete_setup, number=1) | |
os.remove(DB_PATH) | |
delete_bulk_time = timeit.timeit(delete_bulk_stmt, delete_setup, number=1) | |
os.remove(DB_PATH) | |
sorting_setup = delete_setup | |
sorting_stmt = "con.execute('SELECT run FROM runs ORDER BY run ASC')" | |
sorting_time = timeit.timeit(sorting_stmt, sorting_setup, number=int(1e3)) | |
os.remove(DB_PATH) | |
print 'Timing SQLite with {0} runs'.format(NRUNS) | |
print '-'*40 | |
print 'One-by-one insert:', insert_time | |
print 'Bulk insert: ', insert_bulk_time | |
print 'One-by-one delete:', delete_time | |
print 'Bulk delete: ', delete_bulk_time | |
print 'Sorted list: ', sorting_time |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Two sample outputs: