[Python-checkins] r46538 - sandbox/trunk/structbench sandbox/trunk/structbench/bench_struct.py
bob.ippolito
python-checkins at python.org
Tue May 30 01:05:05 CEST 2006
Author: bob.ippolito
Date: Tue May 30 01:05:05 2006
New Revision: 46538
Added:
sandbox/trunk/structbench/
sandbox/trunk/structbench/bench_struct.py (contents, props changed)
Log:
struct benchmarks
Added: sandbox/trunk/structbench/bench_struct.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/structbench/bench_struct.py Tue May 30 01:05:05 2006
@@ -0,0 +1,83 @@
+import struct
+# A representative set of formats from the stdlib
+FORMATS = [(s, struct.calcsize(s)) for s in
+"""
+<H
+<i
+>d
+>l
+>L
+>h
+!HHB
+>h
+>ii
+>H
+>L
+<L
+<i
+<iii
+<I
+<4I
+>4I
+<II
+>II
+<l
+<L
+<i
+<I
+>L
+>L
+<i
+>d
+P
+lxxxxlxxxxlhh
+hhlllii
+hhllhh
+l
+L
+148b
+148B
+356B
+356b
+<L
+<l
+<hhllh
+<h
+<l4s4slhhllhh4s
+<l
+=l
+>L
+>f
+>d
+>l
+<4s4H2lH
+<4s4B4HlLL5HLl
+<4s2B4HlLL2H
+<lLL
+Iiiiiii
+""".splitlines() if s]
+
+import sys
+VERBOSE = '-v' in sys.argv[1:]
+COUNT = 10000
+import time
+t0 = time.time()
+for fmt, size in FORMATS:
+ t = time.time()
+ for i in xrange(COUNT):
+ struct.pack(fmt, *struct.unpack(fmt, '\x00' * size))
+ struct.pack(fmt, *struct.unpack(fmt, '\xcc' * size))
+ if VERBOSE:
+ print '%s: %.2f' % (fmt, time.time() - t,)
+print '[old style]: %.2f' % (time.time() - t0,)
+if hasattr(struct, 'Struct'):
+ OBJS = [(struct.Struct(fmt), size) for fmt, size in FORMATS]
+ t0 = time.time()
+ for s, size in OBJS:
+ t = time.time()
+ for i in xrange(COUNT):
+ s.pack(*s.unpack('\x00' * size))
+ s.pack(*s.unpack('\xff' * size))
+ if VERBOSE:
+ print '%s: %.2f' % (s.format, time.time() - t,)
+ print '[new style]: %.2f' % (time.time() - t0,)
More information about the Python-checkins
mailing list