numpy/numpy — pull request #5111 by larsmans (2014-09-24 14:19:00+00:00)
Title: ENH: faster (branchless) logical_xor

Here's an optimization to `logical_xor` for non-`bool` types (because @jaimefrio already tackled those in 494abcf189e9b53e42b624752eb3eaaf46a0dfd3). Current master:

```python
In [1]: x = (np.random.RandomState(30).randn(1e7) > 0).astype(np.intc)

In [2]: y = (np.random.RandomState(30).randn(1e7) > 0).astype(np.intc)

In [3]: %timeit np.logical_xor(x, y)
10 loops, best of 3: 41.6 ms per loop

In [5]: x = x.astype(np.uint8)

In [6]: x = x.astype(np.uint8)

In [7]: y = y.astype(np.uint8)

In [8]: %timeit np.logical_xor(x, y)
10 loops, best of 3: 40.8 ms per loop
```

This PR, same data:

```python
In [3]: %timeit np.logical_xor(x, y)
100 loops, best of 3: 11.5 ms per loop

In [4]: np.all(np.logical_xor(x, y) == (x != y)) # sanity check
Out[4]: True

In [7]: %timeit np.logical_xor(x, y) # np.uint8
100 loops, best of 3: 9.95 ms per loop
```

For floats the speedup isn't quite as dramatic:

```python
In [9]: x = np.random.RandomState(30).randn(1e7)

In [10]: y = np.random.RandomState(30).randn(1e7)

In [11]: %timeit np.logical_xor(x, y)
10 loops, best of 3: 23.2 ms per loop
```

Now:

```python
In [8]: x = np.random.RandomState(30).randn(1e7)

In [9]: y = np.random.RandomState(30).randn(1e7)

In [10]: %timeit np.logical_xor(x, y)
100 loops, best of 3: 17.4 ms per loop
```

(Then again, who would do a XOR on two float arrays?)