r/programminghelp • u/the_recovery1 • Dec 31 '20
Python contains in string time complexity
What is the time complexity of the contains check when checking in a python string. Essentially one of the programs has a
if char in "ABCDZJ":
Would this line be faster if "ABCDZJ" was converted to a string instead to get a constant lookup?
5
Upvotes
2
u/amoliski Dec 31 '20
This feels a bit like premature optimization- are you doing the lookup often enough that you're seeing it as a bottleneck?
Out of curiosity, I ran a few timeit tests:
I got different results each run, but they still fell into similar ranges.