Advertisement
Guest User

Untitled

a guest
Sep 10th, 2012
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.77 KB | None | 0 0
  1. import collections
  2. import sqlite3
  3. import timeit
  4. import os
  5. import itertools
  6.  
  7. ColsCount = 100
  8. fileName = 'example.db'
  9. filePath = os.path.join(os.path.dirname(os.path.realpath(__file__)), fileName)
  10.  
  11. def createTable(conn, rows=100000, cols=ColsCount):
  12.     c = conn.cursor()
  13.     columns = ",".join("Col{0} text".format(x) for x in xrange(cols))
  14.     values = ",".join("\"Value_{0}\"".format(x) for x in xrange(cols))
  15.     c.execute("CREATE TABLE test (id int, {0} )".format(columns))
  16.     for x in xrange(rows):
  17.         c.execute("INSERT INTO test VALUES ({0}, {1})".format(x, values))
  18.     conn.commit()
  19.  
  20. Columns = tuple(itertools.chain(('Id',), tuple("Col{0}".format(x) for x in xrange(ColsCount))))
  21. TupleClass = collections.namedtuple("TupleClass", Columns)
  22.  
  23. class RowWrapper(object):
  24.     def __init__(self, values):
  25.         self.Id = values[0]
  26.         for x in xrange(ColsCount):
  27.             setattr(self, "Col{0}".format(x), values[x + 1])
  28.  
  29. from django.conf import settings
  30.  
  31. settings.configure(DATABASES =
  32.     {
  33.     'default': {
  34.         'ENGINE': 'django.db.backends.sqlite3',
  35.         'NAME': filePath
  36.         }
  37.     }
  38. )
  39.  
  40. from django.db import models
  41.  
  42. def buildDjangoObject():
  43.     class DjangoObject(models.Model):
  44.         class Meta:
  45.             app_label = ''
  46.     DjangoObject.Id = models.IntegerField()
  47.     for x in xrange(ColsCount):
  48.         setattr(DjangoObject, "Col{0}".format(x), models.TextField())
  49.     return DjangoObject
  50.  
  51. DjangoObject = buildDjangoObject()
  52.  
  53. class Tester:
  54.     query = "SELECT * FROM test"
  55.  
  56.     def __init__(self, conn):
  57.         self.cursor = conn.cursor()
  58.  
  59.     def test1(self):
  60.         return tuple(row for row in self.cursor.execute(self.query))
  61.  
  62.     def test2(self):
  63.         return tuple(RowWrapper(row) for row in self.cursor.execute(self.query))
  64.  
  65.     def test3(self):
  66.         return tuple(TupleClass._make(row) for row in self.cursor.execute(self.query))
  67.  
  68.     def test4(self):
  69.         return tuple(dict(zip(Columns, row)) for row in self.cursor.execute(self.query))
  70.  
  71.     def test5(self):
  72.         return tuple(row for row in DjangoObject.objects.raw(self.query))
  73.  
  74. if __name__ == '__main__':
  75.     try:
  76.         os.remove(filePath)
  77.     except OSError:
  78.         pass
  79.     conn = sqlite3.connect(filePath)
  80.     createTable(conn)
  81.     tester = Tester(conn)
  82.     t1 = timeit.Timer(tester.test1)
  83.     t2 = timeit.Timer(tester.test2)
  84.     t3 = timeit.Timer(tester.test3)
  85.     t4 = timeit.Timer(tester.test4)
  86.     t5 = timeit.Timer(tester.test5)
  87.     print("Sample 1(tuple): {0}".format(t1.timeit(1)))
  88.     print("Sample 2(RowWrapper): {0}".format(t2.timeit(1)))
  89.     print("Sample 3(TupleClass): {0}".format(t3.timeit(1)))
  90.     print("Sample 4(dict): {0}".format(t4.timeit(1)))
  91.     print("Sample 5(django.model + raw()): {0}".format(t5.timeit(1)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement