Skip to content

Instantly share code, notes, and snippets.

@salehi
Forked from methane/bench.py
Created July 20, 2023 08:41
Show Gist options
  • Save salehi/2472d40533a33120fba5300784072ef5 to your computer and use it in GitHub Desktop.
Save salehi/2472d40533a33120fba5300784072ef5 to your computer and use it in GitHub Desktop.
Benchmarking MySQL drivers (Python 3.4)
import time
def query_10k(cur):
t = time.time()
for _ in range(10000):
cur.execute("SELECT 1,2,3,4,5")
res = cur.fetchall()
assert len(res) == 1
assert res[0] == (1,2,3,4,5)
return time.time() - t
def mysql_connector_python():
import mysql.connector
conn = mysql.connector.connect(user='root', host='localhost')
print("MySQL Connector/Python:", query_10k(conn.cursor()), "[sec]")
def mysqlclient():
import MySQLdb
conn = MySQLdb.connect(user='root', host='localhost')
print("mysqlclient:", query_10k(conn.cursor()), "[sec]")
def pymysql():
import pymysql
conn = pymysql.connect(user='root', host='localhost')
print("PyMySQL:", query_10k(conn.cursor()), "[sec]")
mysql_connector_python()
mysqlclient()
pymysql()
MySQL Connector/Python: 4.554934978485107 [sec]
mysqlclient: 0.8555710315704346 [sec]
PyMySQL: 5.129631996154785 [sec]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment