# xor operator

Dom Grigonis dom.grigonis at gmail.com
Wed Nov 15 05:26:32 EST 2023

```Thank you,

test2 = [True] * 100 + [False] * 2
test2i = list(range(100))

%timeit len(set(test2i)) == 1   # 1.6 µs ± 63.6 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
%timeit all(test2)              # 386 ns ± 9.58 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

test2s = set(test2i)
%timeit len(test2s) == 1        # 46.1 ns ± 1.65 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
If you pre-convert to set it is obviously faster. However, set operation is most likely going to be part of the procedure. In which case it ends up to be significantly slower.

Regards,
DG

> On 15 Nov 2023, at 02:34, Peter J. Holzer via Python-list <python-list at python.org> wrote:
>
> 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 <mailto:hjp at hjp.at>         |    -- Charles Stross, "Creative writing
> __/   | http://www.hjp.at/ <http://www.hjp.at/> |       challenge!"
> --
> https://mail.python.org/mailman/listinfo/python-list <https://mail.python.org/mailman/listinfo/python-list>
```