XORing on arrays faster?

rdudfield at my-deja.com rdudfield at my-deja.com
Thu Feb 17 05:29:23 CET 2000


In article <88dokb$ovt at gap.cco.caltech.edu>,
  kern at caltech.edu (Robert Kern) wrote:
> In article <88dhqa$nmi$1 at nnrp1.deja.com>,
> 	rdudfield at my-deja.com writes:
> > Hello,
> >
> > Anyone know how to do XORing on arrays fast?
> >
> > I do not want to use the Numeric package, for other reasons.
>
> Out of curiousity, why not?
>

A few, all superficial really :)

Most of my code uses standard arrays allready.
Numpy does not come with standard python.
Numpy adds extra size to my program.  I'm trying to make it smaller as
is :)


> > So that is
> > out of the question. BTW using Numeric XORing two arrays is more
than
> > four times as fast as this method.
> >
> > a = array.array('I',[0]) * 1000
> > b = array.array('I','\377\377\377\377') * 1000
> >
> > # this is the bit I want done faster :)
> > for x in xrange(1000):
> >   a[x] = a[x] ^ b[x]
> >
> >
> > Also if you know a way to map one sequence onto another fast, I'd
like
> > to know how to do that too :)
>
> Python 1.5.2 (#0, Sep 13 1999, 09:12:57)  [GCC 2.95.1 19990816
(release)] on linux2
> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> >>> import operator
> >>> import array
> >>> a = array.array('I',[0]) * 1000
> >>> b = array.array('I','\377\377\377\377') * 1000
> >>> a = array.array('I', map(operator.xor, a, b))
>
> The in-place modification semantics, I can't help you with.  Anyone
> else?

This is close to Numpy speed, and still uses way less memory than
Numeric.  So I am happy.

If you do use map(xor, a, b) instead of map(operator.xor, a, b) it is
slightly faster, but not by much.


Thanks for your help.

Rene.


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list