I need to build a function that removes all digits from a string in Python, however my question isn't about how to do it (because I know from previous posts here)
I have a question about the complexity of these two functions, one is using filter and lambda function to remove digits and the other is using regex, from basic tests with very long strings (~67,912,000 chars +)
I came to a conclusion that regex is faster, however I do not know the actual complexity and so my 'calculation' is not correct/is biased on computer specs (maybe my computer is fast and other's will be slow or vice versa...)
Reading from the internet about the complexity of the filter function and complexity of regex
I understood that they are both O(n)..
So, which one is 'overkill' in memory/time complexity and which one isn't. Which one should you use (As a better practice)?:
import time
import re
def dwf(word):
start = time.time()
word = ''.join((filter(lambda x: x.isalpha(), word)))
end = time.time()
return end - start
def dwr(word):
start = time.time()
word = re.sub(r"\d+", '', word)
end = time.time()
return end-start
For example, for me:
if the word is 67 million ~chars the difference in time was close to 7 seconds! (when filter is longer and regex is faster)