Skip to content

Instantly share code, notes, and snippets.

@BertrandBordage
Last active July 29, 2021 09:41
Show Gist options
  • Save BertrandBordage/10641643 to your computer and use it in GitHub Desktop.
Save BertrandBordage/10641643 to your computer and use it in GitHub Desktop.
SQLAlchemy vs peewee
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"celltoolbar": "Slideshow",
"name": "",
"signature": "sha256:98b7c5c4af3f267f31c9d2f0199c7622c79d0ccc663454f61eaa283050220545"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"SQLAlchemy vs peewee"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"SQLAlchemy"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from sqlalchemy import *\n",
"from sqlalchemy.ext.declarative import declarative_base\n",
"from sqlalchemy.orm import sessionmaker\n",
"\n",
"\n",
"engine = create_engine('sqlite:///sqlalchemy.db', echo=False)\n",
"Base = declarative_base()\n",
"Session = sessionmaker(bind=engine)\n",
"session = Session()\n",
"\n",
"\n",
"class User(Base):\n",
" __tablename__ = 'users'\n",
"\n",
" id = Column(Integer, primary_key=True)\n",
" name = Column(String(40))\n",
" age = Column(Integer)\n",
" password = Column(String)\n",
"\n",
"\n",
"Base.metadata.create_all(engine)\n",
"\n",
"\n",
"def create_user():\n",
" session.add(User(name='Mary', age=30, password='secret'))\n",
" session.commit()\n",
"\n",
"%timeit create_user()\n",
"%timeit session.query(User).first()\n",
"\n",
"session.close()"
],
"language": "python",
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"10 loops, best of 3: 143 ms per loop\n",
"1000 loops, best of 3: 573 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 3,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"peewee"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from peewee import *\n",
"\n",
"\n",
"db = SqliteDatabase('peewee.db')\n",
"\n",
"\n",
"class User(Model):\n",
" name = CharField(max_length=40)\n",
" age = IntegerField()\n",
" password = CharField()\n",
"\n",
" class Meta:\n",
" database = db\n",
"\n",
"\n",
"User.create_table(fail_silently=True)\n",
"\n",
"\n",
"%timeit User.create(name='Mary', age=30, password='secret')\n",
"%timeit User.select()[0]"
],
"language": "python",
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"10 loops, best of 3: 143 ms per loop\n",
"10000 loops, best of 3: 122 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Comparison"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from __future__ import unicode_literals, division\n",
"sqlalchemy_line_count, peewee_line_count = 31, 20\n",
"print 'Code length: peewee is %.2f\u00d7 shorter than SQLAlchemy' % (sqlalchemy_line_count / peewee_line_count)\n",
"print 'SQL INSERT : peewee is %.2f\u00d7 faster than SQLAlchemy' % (143 / 143)\n",
"print 'SQL SELECT : peewee is %.2f\u00d7 faster than SQLAlchemy' % (573 / 122)"
],
"language": "python",
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Code length: peewee is 1.55\u00d7 shorter than SQLAlchemy\n",
"SQL INSERT : peewee is 1.00\u00d7 faster than SQLAlchemy\n",
"SQL SELECT : peewee is 4.70\u00d7 faster than SQLAlchemy\n"
]
}
],
"prompt_number": 3
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment