# xor operator

Peter J. Holzer hjp-python at hjp.at
Tue Nov 14 19:34:15 EST 2023

```On 2023-11-14 00:11:30 +0200, Dom Grigonis via Python-list wrote:
> Benchmarks:
> test1 = [False] * 100 + [True] * 2
> test2 = [True] * 100 + [False] * 2
>
> TIMER.repeat([
>     lambda: xor(test1),     # 0.0168
>     lambda: xor(test2),     # 0.0172
>     lambda: xor_ss(test1),  # 0.1392
>     lambda: xor_ss(test2),  # 0.0084
>     lambda: xor_new(test1), # 0.0116
>     lambda: xor_new(test2), # 0.0074
>     lambda: all(test1),     # 0.0016
>     lambda: all(test2)      # 0.0046
> ])
> Your first function is fairly slow.
> Second one deals with short-circuiting, but is super slow on full search.
>
> `xor_new` is the best what I could achieve using python builtins.
>
> But builtin `all` has the best performance.

One question worth asking is if a list of bool is the best data
structure for the job. This is essentially a bitmap, and a bitmap is
equivalent to a set of integers. len(s) == 1 is also a fairly quick
operation if s is small. On my system, len(test1s) == 1 (where test1s is
{100, 101}) is about as fast as all(test1) and len(test2s) == 1 (where
test2s is set(range(100))) is about twice as fast as all(test2).

If you are willing to stray from the standard library, you could e.g.
use pyroaring instead of sets: This is about as fast as all(test1)
whether there are two bits set or a hundred.

hp

--
_  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp at hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://mail.python.org/pipermail/python-list/attachments/20231115/9c594916/attachment.sig>
```