Is `=` fast in Clojure?

I would suppose that this depends from case to case how fast = is. Here is some code that tests one particular case:

(defn make-deep-data [n]
  (reduce (fn [dst x]
            [x dst])
          nil
          (range n)))

(def depths (take 12 (iterate (partial * 2) 1)))

(doseq [d depths]
  (let [a (make-deep-data d)
        b (make-deep-data d)]
    (println "\nFor depth d" d)
    (time (= a b))
    (println "Comparing to itself")
    (time (= a a))))

and print outs this

For depth d 1
"Elapsed time: 0.038561 msecs"
Comparing to itself
"Elapsed time: 0.002133 msecs"

For depth d 2
"Elapsed time: 0.003985 msecs"
Comparing to itself
"Elapsed time: 0.001997 msecs"

For depth d 4
"Elapsed time: 0.004792 msecs"
Comparing to itself
"Elapsed time: 0.0012 msecs"

For depth d 8
"Elapsed time: 0.00445 msecs"
Comparing to itself
"Elapsed time: 0.001583 msecs"

For depth d 16
"Elapsed time: 0.009612 msecs"
Comparing to itself
"Elapsed time: 0.002007 msecs"

For depth d 32
"Elapsed time: 0.015544 msecs"
Comparing to itself
"Elapsed time: 0.001894 msecs"

For depth d 64
"Elapsed time: 0.029676 msecs"
Comparing to itself
"Elapsed time: 0.002145 msecs"

For depth d 128
"Elapsed time: 0.057724 msecs"
Comparing to itself
"Elapsed time: 0.001697 msecs"

For depth d 256
"Elapsed time: 0.130804 msecs"
Comparing to itself
"Elapsed time: 0.001664 msecs"

For depth d 512
"Elapsed time: 0.162453 msecs"
Comparing to itself
"Elapsed time: 0.001934 msecs"

For depth d 1024
"Elapsed time: 0.524108 msecs"
Comparing to itself
"Elapsed time: 0.001896 msecs"

For depth d 2048
"Elapsed time: 0.701173 msecs"
Comparing to itself
"Elapsed time: 0.002049 msecs"

So if they are identical (same references) there is no extra costs of them being deep. But if they are equal, but with different memory, there seems to be a cost that depends on the size of the data structure for this particular example. You can do a similar experiment for your use case and that might give you an idea, but I would suppose that = is just what you want in most cases.

It would be nice if someone could point to an article describing more in-depth how Clojure’s equality mechanism works.

1 Like