Last active
January 27, 2021 06:46
-
-
Save cccntu/701f1dcc87fb0fb57b5029697f544e2f to your computer and use it in GitHub Desktop.
simple benchmark/test for python shelve
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 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