Created
July 3, 2018 16:33
-
-
Save jg75/5d3b6de5ce86a9680d632744a3898a21 to your computer and use it in GitHub Desktop.
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
"""A silly test.""" | |
import argparse | |
import inspect | |
import timeit | |
def parse_args(args=None, description=__doc__): | |
"""Parse input arguments.""" | |
parser = argparse.ArgumentParser(description=description) | |
parser.add_argument( | |
"-d", "--delimiter", | |
help="The token delimiter.", | |
default=":" | |
) | |
parser.add_argument( | |
"-t", "--tokens", | |
help="The number of tokens.", | |
type=int, | |
default=100 | |
) | |
parser.add_argument( | |
"-n", "--number", | |
help="The number of tries.", | |
type=int, | |
default=1000000 | |
) | |
return parser.parse_args(args) | |
def get_test_arguments(): | |
"""Get the test arguments.""" | |
arguments = parse_args() | |
string = arguments.delimiter.join([ | |
str(i) for i in range(arguments.tokens) | |
]) | |
return { | |
"setup": "; ".join([ | |
"string='{}'".format(string), | |
"delimiter='{}'".format(arguments.delimiter), | |
"number='{}'".format(arguments.number) | |
]) | |
} | |
def compare(functions, **kwargs): | |
"""Do the compare.""" | |
results = list() | |
message = "{0:<43} {1:.3f} seconds" | |
for function in functions: | |
stmt = inspect.getsource(function) | |
timer = timeit.Timer(stmt=stmt, **kwargs) | |
results.append(timer.timeit()) | |
print(message.format(function.__name__, results[-1])) | |
def test_split_with_limit(string, delimiter): | |
"""First test.""" | |
string.split(delimiter, 2) | |
def test_split_no_limit(string, delimiter): | |
"""Second test.""" | |
string.split(delimiter) | |
if __name__ == "__main__": | |
functions = (test_split_with_limit, test_split_no_limit) | |
kwargs = get_test_arguments() | |
compare(functions, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment