myconnpy, myconnpy + psyco与MySQLdb简单性能测试对比
测试用例修改自:myconnpy appears to be roughly an order of magnitude slower than MySQLdb ,在它的基础上,还加入psyco优化后的性能对比
测试代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""benchmark
"""
from mysql import connector
import MySQLdb
import time
baseq = "SELECT * FROM url limit 10"
def measure(func, *args, **kw):
start = time.time()
result = func(*args, **kw)
end = time.time()
return end - start, result
def scansequential(iters, connect):
for i in xrange(iters):
if MySQLdb.connect == connect:
conn = connect(host='localhost', port=3306, user='aaa', passwd='123', db='test')
else:
conn = connect(host='localhost', port=3306, user='aaa', password='123', db='test')
for i in xrange(5):
curs = conn.cursor()
retval = curs.execute(baseq)
orig_rowcount = curs.rowcount
rows = curs.fetchall()
curs.close()
conn.close()
iters = 100
t, _ = measure(scansequential, iters, connector.Connect)
print "myconnpy", t
t, _ = measure(scansequential, iters, MySQLdb.connect)
print "MySQLdb", t
import psyco
psyco.full()
t, _ = measure(scansequential, iters, connector.Connect)
print "myconnpy with psyco", t
# -*- coding: utf-8 -*-
"""benchmark
"""
from mysql import connector
import MySQLdb
import time
baseq = "SELECT * FROM url limit 10"
def measure(func, *args, **kw):
start = time.time()
result = func(*args, **kw)
end = time.time()
return end - start, result
def scansequential(iters, connect):
for i in xrange(iters):
if MySQLdb.connect == connect:
conn = connect(host='localhost', port=3306, user='aaa', passwd='123', db='test')
else:
conn = connect(host='localhost', port=3306, user='aaa', password='123', db='test')
for i in xrange(5):
curs = conn.cursor()
retval = curs.execute(baseq)
orig_rowcount = curs.rowcount
rows = curs.fetchall()
curs.close()
conn.close()
iters = 100
t, _ = measure(scansequential, iters, connector.Connect)
print "myconnpy", t
t, _ = measure(scansequential, iters, MySQLdb.connect)
print "MySQLdb", t
import psyco
psyco.full()
t, _ = measure(scansequential, iters, connector.Connect)
print "myconnpy with psyco", t
获取10行数据:
myconnpy 2.53226113319
MySQLdb 0.572678804398
myconnpy with psyco 1.79596400261
获取50行数据:
myconnpy 6.3902618885
MySQLdb 1.56350803375
myconnpy with psyco 4.51574015617
获取100行数据:
myconnpy 11.1748549938
MySQLdb 2.54134678841
myconnpy with psyco 7.9241900444
明显myconnpy比MySQLdb慢了5倍之多,获取这是纯python实现的通病吧(弱势),加入psyco后,性能有了提高,但是和MySQLdb还是有3x的差距。
不过有了这个纯python的实现,结合异步IO(非阻塞IO),完全可以将数据访问非阻塞的,让它满足stackless, eventlet, coroutine, diesel, tornado等协程开发。
^_^.