Created
August 13, 2012 15:35
-
-
Save anonymous/3341940 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
import time | |
import sqlite3 | |
import sqlalchemy | |
from sqlalchemy.ext.declarative import declarative_base | |
from sqlalchemy import Column, Integer, String, create_engine | |
from sqlalchemy.orm import scoped_session, sessionmaker | |
Base = declarative_base() | |
DBSession = scoped_session(sessionmaker()) | |
class Customer(Base): | |
__tablename__ = "customer" | |
id = Column(Integer, primary_key=True) | |
name = Column(String(255)) | |
def init_sqlalchemy(dbname = 'sqlite:///sqlalchemy.db'): | |
global engine | |
mysqldb=sqlalchemy.engine.url.URL('mysql', username='xxx', password='xxx', host='0.0.0.0.0', database='Customer') | |
engine = create_engine(mysqldb, echo=False) | |
DBSession.remove() | |
DBSession.configure(bind=engine, autoflush=False, expire_on_commit=False) | |
Base.metadata.drop_all(engine) | |
Base.metadata.create_all(engine) | |
def test_sqlalchemy_orm(n=100000): | |
init_sqlalchemy() | |
t0 = time.time() | |
for i in range(n): | |
customer = Customer() | |
customer.name = 'NAME ' + str(i) | |
DBSession.add(customer) | |
if i % 1000 == 0: | |
DBSession.flush() | |
DBSession.commit() | |
print "SqlAlchemy ORM: Total time for " + str(n) + " records " + str(time.time() - t0) + " secs" | |
def test_sqlalchemy_orm_pk_given(n=100000): | |
init_sqlalchemy() | |
t0 = time.time() | |
for i in range(n): | |
customer = Customer(id=i+1, name="NAME " + str(i)) | |
DBSession.add(customer) | |
if i % 1000 == 0: | |
DBSession.flush() | |
DBSession.commit() | |
print "SqlAlchemy ORM pk given: Total time for " + str(n) + " records " + str(time.time() - t0) + " secs" | |
def test_sqlalchemy_core(n=100000): | |
init_sqlalchemy() | |
t0 = time.time() | |
engine.execute( | |
Customer.__table__.insert(), | |
[{"name":'NAME ' + str(i)} for i in range(n)] | |
) | |
print "SqlAlchemy Core: Total time for " + str(n) + " records " + str(time.time() - t0) + " secs" | |
if __name__ == '__main__': | |
test_sqlalchemy_orm(500) | |
test_sqlalchemy_orm_pk_given(500) | |
test_sqlalchemy_core(500) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment