[Zope3-dev] string agnostic page templates, again

Paul Winkler pw_lists at slinkp.com
Wed Sep 8 15:22:24 EDT 2004


On Wed, Sep 08, 2004 at 10:54:52AM -0400, Fred Drake wrote:
> I suspect the fastest way to tell is to simply attempt conversion with
> unicode().  This assumes nobody is messing with the default encoding;
> it *should* never be anything but ASCII anyway.  ;-)  I'm not sure how
> much specifying "ascii" as the 2nd arg to unicode() affects
> performance.

On my system, adding 'ascii' as the second arg seems to add about 
3% to the run time independent of the size of the first argument,
plus some fixed overhead that is significant with really small
inputs:


length	ratio	difference
------	-----  	----------
      1	1.3538	0.0018
      2	1.3567	0.0018
      4	1.1613	0.0010
      8	1.1640	0.0010
     16	1.1428	0.0010
     32	1.1159	0.0008
     64	1.1042	0.0008
    128	1.0854	0.0008
    256	1.0768	0.0010
    512	1.0607	0.0010
   1024	1.0445	0.0012
   2048	1.0419	0.0018
   4096	1.0381	0.0030
   8192	1.0342	0.0051
  16384	1.0297	0.0089
  32768	1.0353	0.0307
  65536	1.0275	0.0587
 131072	1.0356	0.1655
 262144	1.0398	0.4511
 524288	1.0377	0.8614


... where "length" is the size of the first argument
to unicode(),  "ratio" is (time with "ascii" / time without ascii),
and "difference" is (time with "ascii" - time without ascii).

Naiive test script that spat out the above attached.

-- 

Paul Winkler
http://www.slinkp.com
-------------- next part --------------
import time
import operator

timer = time.time  # time.clock

def runner(*args):    
    count, callable = args[:2]
    r = xrange(count)
    args = args[2:]
    now = timer()
    for i in r:
        callable(args)
    return timer() - now

def avg(alist):
    total = reduce(operator.add, alist)
    return total / len(alist)



results = {}
print "length\tratio\tdifference"
print "------\t-----\t----------"

for i in range(10):
    for j in range(20):
        size = 2 ** j
        repeats = 500
        s = 'a' * size
        t1 = runner(repeats, unicode, s, 'ascii')
        t2 = runner(repeats, unicode, s)
        try:
            ratio = t1 / t2
        except ZeroDivisionError:
            ratio = 1.0
        result = (t1, t2, ratio,  t1 - t2)
        if results.has_key(size):
            results[size].append(result)
        else:
            results[size] = [result]

sizes = results.keys()
sizes.sort()
for size in sizes:
    factors = [result[2] for result in results[size]]
    diffs   = [result[3] for result in results[size]]
    f = avg(factors)
    d = avg(diffs)
    print '%7d\t%0.4f\t%0.4f' % (size, f, d)


        


    



More information about the Zope3-dev mailing list