
# Uncomment the following two lines to use cdecimal globally:
#import sys, cdecimal
#sys.modules["decimal"] = cdecimal

import psycopg2
from decimal import Decimal
from time import time

dbconn = psycopg2.connect(database="dectest", user="decuser",
                          password="changeme")
cur = dbconn.cursor()

# insert 100000 rows
a = "1234567.89"
b = "123.45"
c = "1234567890123.1234567"
d = "12345678901.1234"

start = time()
for i in xrange(100000):
    cur.execute("INSERT INTO numeric_table (a, b, c, d) VALUES (%s, %s, %s, %s)",
                (Decimal(a), Decimal(b), Decimal(c), Decimal(d)))

dbconn.commit()
end = time()
print("INSERT: %f" % (end-start))


# select
start = time()
cur.execute("SELECT * from numeric_table;")
end = time()
print("SELECT: %f" % (end-start))

# fetchall
start = time()
rows = cur.fetchall()
end = time()
print("FETCHALL: %f" % (end-start))

# delete
cur.execute("DELETE from numeric_table;")
dbconn.commit()

