问题
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.
回答1:
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
来源:https://stackoverflow.com/questions/23079774/what-is-the-performance-of-count-on-a-clojure-set