Open mkrd opened 1 year ago
import time
import mmap
def naive_insert_at_beginning():
t1 = time.monotonic()
with open("ddb_storage/users copy.json", "r+b") as f:
data = f.read()
f.seek(0)
f.write(b"aaaa" + data)
f.flush()
t2 = time.monotonic()
print(f"Time taken: {(t2 - t1) * 1000}ms")
# naive_insert_at_beginning()
def mmap_insert_at_beginning():
t1 = time.monotonic()
append_str = b"dddd"
with open("ddb_storage/users copy.json", "a+b") as f:
f.seek(0, 2)
f.write(append_str)
f.flush()
with mmap.mmap(f.fileno(), 0, flags=mmap.MAP_PRIVATE, ) as m:
m.move(0, 4, len(m) - 4)
m.seek(0)
m.write(append_str)
m.flush()
t2 = time.monotonic()
print(f"Time taken: {(t2 - t1) * 1000}ms")
mmap_insert_at_beginning()
mmap could greatly improve the performance of .at(…, key=…).session()