pyList

一个sqlite create insert 性能测试

1000次create,1000000次insert 。测试代码如下,直接运行即可

sqlite create insert test

# -*- coding: utf-8 -*-

from sqlite3 import connect
import os, time, datetime, platform 
path = "D:\\testP.sqlite" 
log = open("D:\\testP.log", "a+") 
con = connect(path) 
def prepare(): 
    global con 
    con.close() 
    try: 
        os.remove(path) 
        print path, 'deleted' 
    except (WindowsError): 
        pass 
    con = connect(path) 

def testCreate(n): 
    c = con.cursor() 
    for i in xrange(n): 
        c.execute("create table test%d (id int)"%(i)) 
    con.commit() 
    c.close() 

def testInsert(n): 
    c = con.cursor() 
    c.execute("create table testinsert (id int)") 
    for i in xrange(n): 
        c.execute("insert into testinsert (id) values (%d)"%(i)) 
    con.commit() 
    c.close() 

def showTime(x, n): 
    begin=datetime.datetime.today() 
    x(n) 
    end  =datetime.datetime.today() 
    print "run %s %d\t times"%(x.func_name,n), end-begin 
    log.write("%s %s run %s %d\t times %s\n"%(platform.node(), 

platform.processor(),x.func_name,n, end-begin)) 

if __name__=='__main__': 
    prepare() 
    showTime(testCreate, 1000) 
    showTime(testInsert, 1000000)
本文网址: https://pylist.com/t/1379513340 (转载注明出处)
如果你有任何建议或疑问可以在下面 留言
Be the first to comment!
Captcha image
Relative Articles