# Optimization: How to make sure XOR is performed in chunks

**URL:** https://discourse.julialang.org/t/optimization-how-to-make-sure-xor-is-performed-in-chunks/33947
**Category:** New to Julia
**Created:** [January 29, 2020, 8:19pm UTC](https://discourse.julialang.org/t/optimization-how-to-make-sure-xor-is-performed-in-chunks/33947 "2020-01-29T20:19:17Z")
**Posts on this page:** 1
**Showing post:** 6

<div class="post-metadata">

### Author: ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)
#### Post date: [January 29, 2020, 10:01pm UTC](https://discourse.julialang.org/t/optimization-how-to-make-sure-xor-is-performed-in-chunks/33947/6 "2020-01-29T22:01:24Z")

</div>

You can get rid of the buffer by implementing a custom function too if you want:

```julia
function hamming(A::BitArray, B::BitArray)
    #size(A) == size(B) || throw(DimensionMismatch("sizes of A and B must match"))
    Ac,Bc = A.chunks, B.chunks
    W = 0
    for i = 1:(length(Ac)-1)
        W += count_ones(Ac[i] ⊻ Bc[i])
    end
    W += count_ones(Ac[end] ⊻ Bc[end] & Base._msk_end(A))
    return W
end

```

This gets you another small speedup:

```julia
julia> @benchmark hamming(x,y,z) setup=(x=bitrand(n);y=bitrand(n);z=bitrand(n))
BenchmarkTools.Trial:
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 28.141 ns (0.00% GC)
  median time: 28.844 ns (0.00% GC)
  mean time: 29.295 ns (0.00% GC)
  maximum time: 109.950 ns (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 995

julia> @benchmark hamming(x,y) setup=(x=bitrand(n);y=bitrand(n))
BenchmarkTools.Trial:
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 21.463 ns (0.00% GC)
  median time: 21.565 ns (0.00% GC)
  mean time: 21.930 ns (0.00% GC)
  maximum time: 240.221 ns (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 997

```

---

_[View the full topic](https://discourse.julialang.org/t/optimization-how-to-make-sure-xor-is-performed-in-chunks/33947)._
