Created
March 10, 2014 11:31
-
-
Save malanjp/9463395 to your computer and use it in GitHub Desktop.
This file contains 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 MySQLdb | |
from datetime import datetime | |
import time | |
limit = 100000 | |
print('insert count {0}'.format(limit)) | |
class Timer(object): | |
def __init__(self, verbose=False): | |
self.verbose = verbose | |
def __enter__(self): | |
self.start = time.time() | |
return self | |
def __exit__(self, *args): | |
self.end = time.time() | |
self.secs = self.end - self.start | |
self.msecs = self.secs * 1000 # millisecs | |
if self.verbose: | |
print('elapsed time: {0} ms').format(self.msecs) | |
connection = MySQLdb.connect(db="bench", user="test") | |
cursor = connection.cursor() | |
""" | |
every commit | |
""" | |
def remove(): | |
cursor.execute("delete from bench") | |
connection.commit() | |
remove() | |
with Timer() as t: | |
for count in range(limit): | |
cursor.execute("insert into bench (text, date) values ('{0}', '{1}')".format('testtext', datetime.utcnow())) | |
connection.commit() | |
print('every commit =', t.secs) | |
""" | |
one commit | |
""" | |
remove() | |
with Timer() as t: | |
for count in range(limit): | |
cursor.execute("insert into bench (text, date) values ('{0}', '{1}')".format('testtext', datetime.utcnow())) | |
connection.commit() | |
print('atomic =', t.secs) | |
cursor.close() | |
connection.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment