# Efficient way of comparing two matrices

**URL:** <https://discourse.julialang.org/t/efficient-way-of-comparing-two-matrices/36044>\
**Category:** Performance\
**Created:** [March 16, 2020, 3:48pm UTC](https://discourse.julialang.org/t/efficient-way-of-comparing-two-matrices/36044 "2020-03-16T15:48:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmcastro2109](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmcastro2109/32/38427_2.png) [@jmcastro2109](https://discourse.julialang.org/u/jmcastro2109)\
**Post date:** [March 16, 2020, 3:48pm UTC](https://discourse.julialang.org/t/efficient-way-of-comparing-two-matrices/36044/1 "2020-03-16T15:48:46Z")

</div>

Hello, this question might seem very naive. But what is the best way of doing a logical comparison of two matrices. Consider the following MWE:

```julia
function compare(N)
    for n=1:N
        X=rand([0,1],4,4)
        Y=rand([0,1],4,4)
        if X != Y
            println("These matrices are different")
        end
    end
end

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [March 16, 2020, 4:15pm UTC](https://discourse.julialang.org/t/efficient-way-of-comparing-two-matrices/36044/2 "2020-03-16T16:15:21Z")

</div>

> [@jmcastro2109](#):
>
> X=rand([0,1],4,4) Y=rand([0,1],4,4)

What about this doesn’t work? That all looks fine to me.

---

<div class="post-metadata">

**Author:** ![jmcastro2109](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmcastro2109/32/38427_2.png) [@jmcastro2109](https://discourse.julialang.org/u/jmcastro2109)\
**Post date:** [March 16, 2020, 4:34pm UTC](https://discourse.julialang.org/t/efficient-way-of-comparing-two-matrices/36044/3 "2020-03-16T16:34:09Z")

</div>

In principle, everything works.

I was just wondering whether

```julia

X != Y

```

is the most efficient way to do the logical comparison of the matrices.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [March 16, 2020, 4:36pm UTC](https://discourse.julialang.org/t/efficient-way-of-comparing-two-matrices/36044/4 "2020-03-16T16:36:16Z")

</div>

It almost certainly is, and if someone finds something faster and reports it, a pull request will be made in Julia to make that faster way be the way `X!=Y` is computed.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 17, 2020, 9:35am UTC](https://discourse.julialang.org/t/efficient-way-of-comparing-two-matrices/36044/5 "2020-03-17T09:35:45Z")

</div>

If you check (eg with `@edit`) you will find that it ultimately dispatches to [`(==)(A::AbstractArray, B::AbstractArray)`](https://github.com/JuliaLang/julia/blob/36241a90bab7256f83392dc302808f04954a1f3b/base/abstractarray.jl#L1778) which is written efficiently (in the sense that it will return early if it finds a discrepancy).

Generally, if you are concerned about efficiency, it is best to just learn about the relevant tools — the [performance tips](https://docs.julialang.org/en/v1/manual/performance-tips/) is a good starting point for efficiency of compiled code, while for algorithmic efficiency you should just learn to understand Julia code.
