Last active
April 14, 2022 14:42
-
-
Save rnag/7c2e0cbd76a0af484009428d03b7bff2 to your computer and use it in GitHub Desktop.
trim_extra_whitespace_times
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import string | |
from time import time | |
def main(): | |
# Rust version run with `--release` (for reference), using `only_one_string` from here: | |
# https://stackoverflow.com/a/71864244/10237506 | |
# trim_whitespace: 30ms | |
# Python results (tested on Python 3.10.0 on Mac OS X, Intel processor) | |
# trim_whitespace_v1: 85ms | |
# trim_whitespace_v2: 413ms | |
# trim_whitespace_v3: 87ms | |
funcs = [trim_whitespace_v1, trim_whitespace_v2, trim_whitespace_v3] | |
func_names = [f.__name__ for f in funcs] | |
n = 100_000 # run for this many iterations | |
for name in func_names: | |
fn = globals()[name] | |
start = time() | |
for i in range(n): | |
actual = fn("1 | This is a “TEST” | Testing 123 | H e l l o world! ") | |
print(f'{name}: {(time() - start) * 1_000:.0f}ms') | |
for input, expected in [ | |
("testing123!", "testing123!"), | |
("testing 123!", "testing 123!"), | |
("testing 123! ", "testing 123!"), | |
(" testing 12 3! ", "testing 12 3!") | |
]: | |
for name in func_names: | |
fn = globals()[name] | |
actual = fn(input) | |
assert actual == expected, f'func={name}\n actual={actual!r}\n expected={expected!r}' | |
def trim_whitespace_v1(s: str) -> str: | |
# remove leading and trailing spaces | |
s = s.strip() | |
# replace two spaces with a single space | |
while ' ' in s: | |
s = s.replace(' ', ' ') | |
# return the cleaned string | |
return s | |
def trim_whitespace_v2(s: str) -> str: | |
return re.sub(r' +', ' ', s.strip()) | |
def trim_whitespace_v3(s: str) -> str: | |
# be warned that this removes "all whitespace characters (space, tab, | |
# newline, return, form feed)" | |
return ' '.join(s.split()) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment