Skip to content

Instantly share code, notes, and snippets.

@KentaYamada
Last active June 4, 2016 05:44
Show Gist options
  • Save KentaYamada/873047ef2d68da6f10430291075d0b2f to your computer and use it in GitHub Desktop.
Save KentaYamada/873047ef2d68da6f10430291075d0b2f to your computer and use it in GitHub Desktop.
一度読み込んだSQLファイルをメモ化を使ってキャッシング
import sqlite3
queries = {}
def test1():
sql = ""
with open("./test.sql") as f:
sql = f.read()
with sqlite3.connect("./test.db") as db:
cursor = db.cursor()
cursor.execute(sql)
db.commit()
def test2():
sql = ""
if "test" in queries:
sql = queries["test"]
else:
with open("./test.sql") as f:
sql = f.read()
queries["test"] = sql
with sqlite3.connect("./test.db") as db:
cursor = db.cursor()
cursor.execute(sql)
db.commit()
create table persons (
name text not null
,age integer not null
);
insert into persons (name, age) values ('test', 20);
# coding: -*- utf-8 -*-
import time
import pysqlite3
if __name__ == "__main__":
start = time.time()
for i in range(0, 10000):
pysqlite3.test1()
elapsed_time = time.time() - start
print("処理時間: {0}".format(elapsed_time))
# coding: -*- utf-8 -*-
import time
import pysqlite3
if __name__ == "__main__":
start = time.time()
for i in range(0, 10000):
pysqlite3.test2()
elapsed_time = time.time() - start
print("処理時間: {0}".format(elapsed_time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment