Skip to content

Instantly share code, notes, and snippets.

@ringerc
Created October 3, 2015 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ringerc/7642350fee9f729d1466 to your computer and use it in GitHub Desktop.
Save ringerc/7642350fee9f729d1466 to your computer and use it in GitHub Desktop.
Compare disconnect/reconnect and pooled times
#!/usr/bin/env python
#
# Disconnect/reconnect each query
#
# Takes 0.45s to run here
#
import time
import psycopg2
start_t = time.time()
for x in range(1, 100):
conn = psycopg2.connect('dbname=test')
curs = conn.cursor()
curs.execute("SELECT 1;")
curs.fetchall()
curs.close()
conn.close()
end_t = time.time()
print "Elapsed: ", end_t - start_t
#!/usr/bin/env python
#
# Re-use connection
#
# Takes 0.01s to run here
import time
import psycopg2
start_t = time.time()
conn = psycopg2.connect('dbname=test')
for x in range(1, 100):
curs = conn.cursor()
curs.execute("SELECT 1;")
curs.fetchall()
curs.close()
conn.close()
end_t = time.time()
print "Elapsed: ", end_t - start_t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment