Guest User

Untitled

a guest
Jun 12th, 2020
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.60 KB | None | 0 0
  1. import mysql.connector
  2. import time
  3. from tqdm import tqdm
  4.  
  5.  
  6. def db_connect():
  7.     return mysql.connector.connect(host='localhost',
  8.                                    database='MYDATABASE',
  9.                                    user='root',
  10.                                    password='MYROOT')
  11.  
  12.  
  13. def time_in_ms():
  14.     return int(round(time.time() * 1000))
  15.  
  16.  
  17. repeats = 100
  18.  
  19. connection = db_connect()
  20. cursor = connection.cursor()
  21. print('NORMAL')
  22. print('cursor', type(cursor))
  23. start = time_in_ms()
  24. for i in tqdm(range(repeats)):
  25.     cursor.execute('DELETE FROM test')
  26.     connection.commit()
  27.     cursor.execute("INSERT into test (id,name) VALUES (1,'Value 1')")
  28.     cursor.execute("INSERT into test (id,name) VALUES (2,'Value 2')")
  29.     cursor.execute("INSERT into test (id,name) VALUES (3,'Value 3')")
  30.     cursor.execute("INSERT into test (id,name) VALUES (4,'Value 4')")
  31.     cursor.execute("INSERT into test (id,name) VALUES (5,'Value 5')")
  32.     connection.commit()
  33. print('Time:', time_in_ms() - start)
  34. cursor.close()
  35. connection.close()
  36.  
  37.  
  38. connection = db_connect()
  39. cursor = connection.cursor(prepared=True)
  40. print('\n\nPREPARED')
  41. print('cursor', type(cursor))
  42. start = time_in_ms()
  43. for i in tqdm(range(repeats)):
  44.     cursor.execute('DELETE FROM test')
  45.     connection.commit()
  46.     sql = "INSERT into test (id,name) VALUES (%s,%s)"
  47.     cursor.execute(sql, (1, 'Value 1'))
  48.     cursor.execute(sql, (2, 'Value 2'))
  49.     cursor.execute(sql, (3, 'Value 3'))
  50.     cursor.execute(sql, (4, 'Value 4'))
  51.     cursor.execute(sql, (5, 'Value 5'))
  52.     connection.commit()
  53. print('Time:', time_in_ms() - start)
Add Comment
Please, Sign In to add comment