Permalink
Browse files

Add more benchmarks

  • Loading branch information...
tibbe committed Aug 23, 2012
1 parent 54e9052 commit 737cad93e04c8ec5913e194d37d87153dfae40db
Showing with 120 additions and 22 deletions.
  1. +33 −9 benchmarks/Benchmarks.hs
  2. +87 −13 benchmarks/bench.py
View
@@ -24,23 +24,47 @@ instance FromNamedRecord President where
m .: "Party" <*>
m .: "Home State"
+instance ToNamedRecord President where
+ toNamedRecord (presidency, president, wikipediaEntry, tookOffice,
+ leftOffice, party, homeState) = namedRecord
+ [ "Presidency" .= presidency
+ , "President" .= president
+ , "Wikipedia Entry" .= wikipediaEntry
+ , "Took office" .= tookOffice
+ , "Left office" .= leftOffice
+ , "Party" .= party
+ , "Home State" .= homeState
+ ]
+
fromStrict s = BL.fromChunks [s]
main :: IO ()
main = do
!csvData <- fromStrict `fmap` B.readFile "benchmarks/presidents.csv"
- !csvDataH <- fromStrict `fmap` B.readFile
+ !csvDataN <- fromStrict `fmap` B.readFile
"benchmarks/presidents_with_header.csv"
+ let (Right !presidents) = decodePresidents csvData
+ (Right (!hdr, !presidentsN)) = decodePresidentsN csvDataN
defaultMain [
- bgroup "indexed" [
- bench "presidents/without conversion" $ whnf idDecode csvData
- , bench "presidents/with conversion" $ whnf decodePresidents csvData
- ]
- , bgroup "named" [
- bench "presidents/without conversion" $ whnf idDecodeN csvDataH
- , bench "presidents/with conversion" $ whnf decodePresidentsN csvDataH
- ]
+ bgroup "positional"
+ [ bgroup "decode"
+ [ bench "presidents/without conversion" $ whnf idDecode csvData
+ , bench "presidents/with conversion" $ whnf decodePresidents csvData
+ ]
+ , bgroup "encode"
+ [ bench "presidents/with conversion" $ whnf encode presidents
+ ]
+ ]
+ , bgroup "named"
+ [ bgroup "decode"
+ [ bench "presidents/without conversion" $ whnf idDecodeN csvDataN
+ , bench "presidents/with conversion" $ whnf decodePresidentsN csvDataN
+ ]
+ , bgroup "encode"
+ [ bench "presidents/with conversion" $ whnf (encodeByName hdr) presidentsN
+ ]
+ ]
]
where
decodePresidents :: BL.ByteString -> Either String (Vector President)
View
@@ -7,60 +7,134 @@
# (http://seepeoplesoftware.com/downloads/older-versions/11-sample-csv-file-of-us-presidents.html)
import csv
+import cStringIO
import time
-def bench(data):
+def decode(data):
r = csv.reader(data.splitlines())
res = []
- for (presidency, president, wikipedia_entry, took_office, left_office, party, home_state) in r:
+ for (presidency, president, wikipedia_entry, took_office, left_office,
+ party, home_state) in r:
res.append((int(presidency), unicode(president, 'utf-8'),
- wikipedia_entry, took_office, left_office, unicode(party),
- unicode(home_state)))
+ wikipedia_entry, took_office, left_office,
+ unicode(party, 'utf-8'), unicode(home_state, 'utf-8')))
return res
-def bench_named(data):
+def encode(data):
+ buf = cStringIO.StringIO()
+ r = csv.writer(buf)
+ for (presidency, president, wikipedia_entry, took_office, left_office,
+ party, home_state) in data:
+ # str is automatically called
+ r.writerow([presidency, president.encode('utf-8'),
+ wikipedia_entry, took_office, left_office,
+ party.encode('utf-8'), home_state.encode('utf-8')])
+ res = buf.getvalue()
+ buf.close()
+ return res
+
+def decode_named(data):
r = csv.DictReader(data.splitlines())
res = []
for d in r:
res.append((int(d['Presidency']), unicode(d['President'], 'utf-8'),
d['Wikipedia Entry'], d['Took office'], d['Left office'],
- unicode(d['Party']), unicode(d['Home State'])))
+ unicode(d['Party'], 'utf-8'),
+ unicode(d['Home State'], 'utf-8')))
+ return res
+
+def encode_named(data):
+ buf = cStringIO.StringIO()
+ r = csv.DictWriter(buf, ['Presidency', 'President', 'Wikipedia Entry',
+ 'Took office', 'Left office', 'Party',
+ 'Home State'])
+ for d in data:
+ # This does less work than the Haskell code as unicode fields
+ # are not utf-8 encoded but rather ascii encoded since string
+ # is called (unless someone change the locale)
+ r.writerow(d)
+ res = buf.getvalue()
+ buf.close()
+ return res
+
+def as_typed_dicts(data):
+ type_map = {'Presidency': int, 'President': unicode, 'Wikipedia Entry': str,
+ 'Took office': str, 'Left office': str, 'Party': unicode,
+ 'Home State': unicode}
+ r = csv.DictReader(data.splitlines())
+ res = []
+ for d in r:
+ for k in d:
+ typ = type_map[k]
+ if typ == unicode:
+ d[k] = unicode(d[k], 'utf-8')
+ else:
+ d[k] = typ(d[k])
+ res.append(d)
return res
csv_file = open('benchmarks/presidents.csv', 'rb')
csv_data = csv_file.read()
csv_file.close()
+presidents = decode(csv_data)
+
csv_file_named = open('benchmarks/presidents_with_header.csv', 'rb')
csv_data_named = csv_file_named.read()
csv_file_named.close()
+presidents_named = as_typed_dicts(csv_data_named)
+
iters = 10000
-print 'indexed'
+# Positional
+
+print 'positional'
+
+print ' decode'
-print 'Without type conversions:'
+print ' without conversion:',
start = time.time()
for i in xrange(iters):
list(csv.reader(csv_data.splitlines()))
print "%f us" % (1000000 * (time.time() - start) / iters)
-print 'Including type conversions:'
+print ' with conversion:',
start = time.time()
for i in xrange(iters):
- bench(csv_data)
+ decode(csv_data)
print "%f us" % (1000000 * (time.time() - start) / iters)
+print ' encode'
+
+print ' with conversion:',
+start = time.time()
+for i in xrange(iters):
+ encode(presidents)
+print "%f us" % (1000000 * (time.time() - start) / iters)
+
+# Named
+
print 'named'
-print 'Without type conversions:'
+print ' decode'
+
+print ' without conversion:',
start = time.time()
for i in xrange(iters):
list(csv.DictReader(csv_data.splitlines()))
print "%f us" % (1000000 * (time.time() - start) / iters)
-print 'Including type conversions:'
+print ' with conversion:',
+start = time.time()
+for i in xrange(iters):
+ decode_named(csv_data_named)
+print "%f us" % (1000000 * (time.time() - start) / iters)
+
+print ' encode'
+
+print ' with conversion:',
start = time.time()
for i in xrange(iters):
- bench_named(csv_data_named)
+ encode_named(presidents_named)
print "%f us" % (1000000 * (time.time() - start) / iters)

0 comments on commit 737cad9

Please sign in to comment.