XORing long strings opimization?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Martelli

    #16
    Re: XORing long strings opimization?

    Georgy Pruss wrote:
    [color=blue]
    > Well, nothing to add actually, but my results:[/color]
    ...but of course when in a hurry one should remember...
    psyco
    !!!

    So I gave your script a little tweak...:
    [color=blue]
    > fns = [ xorstring0, xorstring1, xorstring2, xorstring3, xorstring4 ][/color]

    i.e. right here I added:

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

    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).
    [color=blue]
    > # 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![/color]

    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

    Comment

    • Paul Rubin

      #17
      Re: XORing long strings opimization?

      Noen <not.available@ na.no> writes:[color=blue]
      > This way is very slow for large strings.
      > Anyone know of a better and faster way to do it?[/color]

      Yes, use the array module.

      Comment

      • Georgy Pruss

        #18
        Re: XORing long strings opimization?

        I didn't even know about psyco. Thanks!
        G-:

        "Alex Martelli" <aleax@aleax.it > wrote in message news:TJ7qb.9659 9$e5.3539510@ne ws1.tin.it...[color=blue]
        > Georgy Pruss wrote:
        >[color=green]
        > > Well, nothing to add actually, but my results:[/color]
        > ...but of course when in a hurry one should remember...
        > psyco
        > !!!
        >
        > <...>[/color]
        [color=blue]
        >
        > Alex
        >[/color]


        Comment

        Working...