What's the time complexity of Python's collections.Counter.total()? I've read the documentation for the method, but there's not mention of its efficiency. Does anyone know how the method is implemented under the hood and what its time complexity is?
-
3There are many implementations of Python.Open AI - Opting Out– Open AI - Opting Out2021-12-02 23:45:24 +00:00Commented Dec 2, 2021 at 23:45
-
Almost certainly it is linear, although I suppose the total could be calculated each time the dict is modified and then the method just retrieves this total, but I doubt itjuanpa.arrivillaga– juanpa.arrivillaga2021-12-02 23:51:15 +00:00Commented Dec 2, 2021 at 23:51
Add a comment
|
1 Answer
In CPython, it looks like it implements total() using sum(self.values()), so it's O(number of values in the Counter).