Runtime of python's if substring in string

前端 未结 4 552
名媛妹妹
名媛妹妹 2020-12-05 18:43

What is the big O of the following if statement?

if \"pl\" in \"apple\":
   ...

What is the overall big O of how python determ

4条回答
  •  执念已碎
    2020-12-05 19:28

    You can use timeit and test it yourself:

    maroun@DQHCPY1:~$ python -m timeit 's = "apple";s.find("pl")'
    10000000 loops, best of 3: 0.125 usec per loop
    maroun@DQHCPY1:~$ python -m timeit 's = "apple";"pl" in s'
    10000000 loops, best of 3: 0.0371 usec per loop
    

    Using in is indeed faster (0.0371 usec compared to 0.125 usec).

    For actual implementation, you can look at the code itself.

提交回复
热议问题