XORing long strings opimization?

Alex Martelli aleax at aleax.it
Wed Nov 5 09:23:47 EST 2003


Georgy Pruss wrote:

> Well, nothing to add actually, but my results:
   ...but of course when in a hurry one should remember...
       psyco
        !!!

So I gave your script a little tweak...:

> fns = [ xorstring0, xorstring1, xorstring2, xorstring3, xorstring4 ]

i.e. right here I added:

import psyco
psyfns = [ psyco.proxy(f) for f in fns ]
fns.extend(psyfns)

so each function is tried in both the plain and psycotic form --
the latter is at an index 5 above (fn5 is the psycoized xorstring0,
etc etc up to fn9 which is the psycoed xorstring4).

> # fn0 -- 5.578 sec -- 6.37
> # fn1 -- 4.593 sec -- 5.25
> # fn2 -- 2.609 sec -- 2.98
> # fn3 -- 2.531 sec -- 2.89
> # fn4 -- 0.875 sec -- 1.00
> 
> BTW, for the same million char strings, a C function runs 0.0035 sec, or
> 250 times faster!

My measurements give:

fn0 -- 19.229 sec -- 347.98
fn1 -- 12.489 sec -- 226.01
fn2 -- 4.719 sec -- 85.39
fn3 -- 4.359 sec -- 78.88
fn4 -- 2.046 sec -- 37.02

fn5 -- 16.736 sec -- 302.86
fn6 -- 9.675 sec -- 175.08
fn7 -- 1.045 sec -- 18.90
fn8 -- 1.048 sec -- 18.96
fn9 -- 0.055 sec -- 1.00

so, my machine is quite a bit slower (it IS getting long in the tooth --
over 30 months -- and wasn't the absolute speediest even then;-) by a
factor that's quite variable, 1.72 to 3.45 times, median 2.34 times.

psyco helps marginally on some approaches, 4+ times on others, and
almost 40 times on the fn4 approach, which happens to be the fastest
even without it (luckily...:-).  Still not a match for your reported
C function, but just 6-7 times slower (guessing), not 250 times.

One thing worth checking is whether the end result IS needed as a
string, because some more time might be saved by e.g. using buffer(x)
rather than x.tostring() for the result array x, in many cases.

Another possibility to be always kept in mind for bulk array operations
is Numeric.  xor-ing 10,000-long byte arrays takes about 10 msec with
array.array (needing a Python-level loop) versus 50 microseconds with
Numeric.array (no loop needed), so a factor of 200 (over the fastest
python-cum-standard-library only approach) is easily available and may
be getting even closer than psyco to bare-C speeds.


Alex





More information about the Python-list mailing list