I'm studying algorithms and I have a question about the difference between isdigit() and my_isdigit().
import time
def my_isdigit(str):
s = set(['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'])
for x in str:
if x not in s:
return False
return True
test_str = '1234567890' * 10000000
start = time.time()
test_str.isdigit()
print(time.time() - start) # 0.21125102043151855
start = time.time()
my_isdigit(test_str)
print(time.time() - start) # 2.169161319732666
Why is isdigit() much faster than mine?
I saw What is the fastest way to make sure a str is a int? but I cannot get the reason.
Is it the difference between C and Python? Or is my code bad?