Last active
June 4, 2016 05:44
-
-
Save KentaYamada/873047ef2d68da6f10430291075d0b2f to your computer and use it in GitHub Desktop.
一度読み込んだSQLファイルをメモ化を使ってキャッシング
This file contains hidden or 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 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() |
This file contains hidden or 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
create table persons ( | |
name text not null | |
,age integer not null | |
); |
This file contains hidden or 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
insert into persons (name, age) values ('test', 20); |
This file contains hidden or 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
# 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)) |
This file contains hidden or 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
# 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