Skip to content

Instantly share code, notes, and snippets.

@cccntu
Last active January 27, 2021 06:46
Show Gist options
  • Save cccntu/701f1dcc87fb0fb57b5029697f544e2f to your computer and use it in GitHub Desktop.
Save cccntu/701f1dcc87fb0fb57b5029697f544e2f to your computer and use it in GitHub Desktop.
simple benchmark/test for python shelve
import shelve
import time
# initialize
with shelve.open('shelvedb') as db:
for i in range(10000):
db[f'{i}'] = '{i*2}'
# no caching
with shelve.open('shelvedb') as db:
tic = time.time()
for i in range(10000):
x = db[f'{i}']
tok = time.time()
print(tok-tic)
tic=tok
for i in range(10000):
x = db[f'{i}']
tok = time.time()
print(tok-tic)
tic=tok
# has caching
with shelve.open('shelvedb', writeback=True) as db:
tic = time.time()
for i in range(10000):
x = db[f'{i}']
tok = time.time()
print(tok-tic)
tic=tok
for i in range(10000):
x = db[f'{i}']
tok = time.time()
print(tok-tic)
tic=tok
# output:
"""
0.06999635696411133
0.06881237030029297
0.06987953186035156
0.0024607181549072266
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment