0

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?

2
  • 3
    There are many implementations of Python. Commented 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 it Commented Dec 2, 2021 at 23:51

1 Answer 1

5

In CPython, it looks like it implements total() using sum(self.values()), so it's O(number of values in the Counter).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.