[Python-checkins] python/nondist/sandbox/datetime picklesize.py,NONE,1.1

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Tue, 03 Dec 2002 09:50:12 -0800


Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory sc8-pr-cvs1:/tmp/cvs-serv26946

Added Files:
	picklesize.py 
Log Message:
New program just to display pickle sizes.  This makes clear that the
copy_reg based C implementation is much more space-efficient in the
end than the __getstate__/__setstate__ based Python implementation,
but that 4-byte date objects still suffer > 10 bytes of overhead each
no matter how many of them you pickle in one gulp.

It also makes clear a bug in timedelta:  str() doesn't work the same
across the implementations.


--- NEW FILE: picklesize.py ---
import datetime, _datetime
if 1:
    import cPickle as pickle
else:
    import pickle

cases = (datetime, "Python"), (_datetime, "C")

def pickleit(thing):
    s = pickle.dumps(thing, 1)
    return len(s)

typenames = "date", "datetime", "timedelta"

for typename in typenames:
    for mod, way in cases:
        obj = getattr(mod, typename)(2000, 12, 13)
        print "pickling", obj, "via", way, "-- pickle length", pickleit(obj)

for typename in typenames:
    for i in range(1, 11):
         for mod, way in cases:
            ctor = getattr(mod, typename)
            objs = [ctor(j+1, 3, 4) for j in range(i**2)]
            plen = pickleit(objs)
            print "list of %3d %ss via %6s -- %4d bytes, %5.2f bytes/obj" % (
                  len(objs), typename, way, plen, float(plen)/len(objs))