Performance describes how efficiently software runs in time and computational-resource use. Compare alternative implementations, then test and inspect the generated code before adopting it. section 2.5.3
Timing two text-cleaning functions
Time clean_text1 and clean_text2 on the same file, run the comparison several times, and use the observed result to choose an implementation. Neither function is slow, but clean_text2 appears faster. Treat that result as evidence for this task, not as a substitute for testing, profiling, and debugging. section 2.5.3
def clean_text1(text):
"""Converts the text to lowercase and removes punctuation"""
text = text.lower()
punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
for character in punctuation:
text = text.replace(character, '')
return text
def clean_text2(text):
"""Converts the text to lowercase and removes punctuation"""
# convert to lowercase
text = text.lower()
# remove punctuation
punctuation = ['.', ',', ';', ':', "'", '"', '!', '?', '-', '(', ')']
for punc in punctuation:
text = text.replace(punc, '')
return text
Compare the two punctuation-removal implementations whose execution speed is measured. section 2.5.3







































