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

℡╲_俬逩灬. 提交于 2019-12-23 09:33:50

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!