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,)
