Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the performance of `count` on a Clojure set?

Tags:

clojure

So, I read that the count operation is O(1) for a Clojure vectors, lists and maps.

(count [1 2 3]) ;=> 3

But is it also O(1) for a Clojure set? I imagine it probably is, but I'm not really sure how to find out. I had a quick read of http://clojure.org/data_structures#Data%20Structures-Sets, but couldn't see the info there.

like image 419
Daniel Neal Avatar asked Apr 15 '14 09:04

Daniel Neal


1 Answers

It is O(1)

You can verify this by observing that clojure.lang.PersistentSet maintains a _count field in the Java source code:

https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/PersistentList.java

like image 193
mikera Avatar answered Sep 20 '22 03:09

mikera