Efficiently check if an element occurs at least n times in a list

后端 未结 7 2202
南旧
南旧 2020-12-07 00:23

How to best write a Python function (check_list) to efficiently test if an element (x) occurs at least n times in a list (l

7条回答
  •  长情又很酷
    2020-12-07 01:07

    Ultimately short circuiting is the way to go if you expect a significant number of cases will lead to early termination. Let's explore the possibilities:

    Take the case of the list.index method versus the list.count method (these were the two fastest according to my testing, although ymmv)

    For list.index if the list contains n or more of x and the method is called n times. Whilst within the list.index method, execution is very fast, allowing for much faster iteration than the custom generator. If the occurances of x are far enough apart, a large speedup will be seen from the lower level execution of index. If instances of x are close together (shorter list / more common x's), much more of the time will be spent executing the slower python code that mediates the rest of the function (looping over n and incrementing i)

    The benefit of list.count is that it does all of the heavy lifting outside of slow python execution. It is a much easier function to analyse, as it is simply a case of O(n) time complexity. By spending almost none of the time in the python interpreter however it is almost gaurenteed to be faster for short lists.

    Summary of selection criteria:

    • shorter lists favor list.count
    • lists of any length that don't have a high probability to short circuit favor list.count
    • lists that are long and likely to short circuit favor list.index

提交回复
热议问题