Package: timetester
timetesterFeed it a method and it tells you the average time.Downloadspip install timetesterorpython3 -m pip install timetesterUsageTimetester Objectimporttimetesterdeffoo(arg):print(arg)passk=timetester.timeTester(foo)# NOT NECESSARY. Only use if you encountered an error and reloaded the objectk.initialise()# Run testsk.runtests(*args,**kwargs)# Graph the average time takenk.graph()# Print a report of how the function didk.report()Timeout Decoratorimporttimetesterimporttime# Raises error@timetester.timeout(10)deffoo(bar):time.sleep(11)Compare objectThe compared functions should have the same intended purpose and the same argumentsimporttimetesterdeffoo():passdefbar():passk=timetester.compare(foo,bar)# NOT NECESSARY. Only use if you encountered an error and reloaded the functionk.initialise()# Arguments are passed in herek.compareFuncs(*args,**kwargs)# Outputs a graph comparing the timek.graph()# Creates a json file consising of the time took for each function to runk.output_asfile()# Prints each function on the screenk.sort()Usage in Real-Time IDE(Terminal)Joining of functions are generally allowed for timeTester object
However, you have to run the actual test function firstimporttimetesterdeffoo(args):passdefbar(args):pass# Allowedtimetester.timeTester(foo).initialise().runtests().graph().report()timetester.timeTester(foo).runtests().graph().report()timetester.compare(foo,bar).initialise().compareFuncs().graph().sort().output_asfile()timetester.compare(foo,bar).compareFuncs().output_asfile().graph()# Not allowed -- You have to run tests firsttimetester.timeTester(foo).graph().report()timetester.timeTester(foo).report()timetester.compare(foo,bar).initialise().graph().sort().output_asfile()timetester.compare(foo,bar).graph().sort()Default optionsimporttimetesterdeffoo():passdefbar():pass@timetester.timeout(seconds=10,error_message=os.strerror(errno.ETIME))k=timetester.timeTester(foo,target=1,print_output=False,runtime=100,maxtime=10,error_time=0,return_type='mean')# compare object arguments must be methods# variables can only be changed laterc=timetester.compare(foo,bar)timeTester object optionstarget(float/int)The target time you want the function to be atimporttimetesterdeffoo(arg):passk=timetester.timeTester(foo,target=0.005)# ork.target=0.05runtime(int,float)How many times the function is ran before taking averageimporttimetesterdeffoo(arg):passk=timetester.timeTester(foo,runtime=100)# ork.runtime=100print_output(bool)disable allprintfunction inside the provided function.importtimetesterdeffoo(arg):print(arg)k=timetester.timeTester(foo,print_output=False)# ork.print_output=False# Even though the function is intended to print arguments, no output will be printed during run testsk.runtests('hi','bye')# After runtests print functions will be normal.print('hi')maxtime(int/float)Maximum time all functions would runimporttimetesterdeffoo(arg):sleep(1)k=timetester.timeTester(foo,maxtime=1)# ork.maxtime=1k.runtests()# When the total time is larger than 1 second error will be raisedreturn_type(choice)Type of value returnedTypes of possible values include:'mean''mode''median''harmonicmean''geometricmean'importtimetesterdeffoo(arg):passk=timetester.timeTester(foo,return_type='geometricmean')# ork.type='geometricmean'timeTester object compatibilityAllows comparision withint,floatanddecimal.Decimalobjects
Take this code below:importtimetesterfromdecimalimportDecimaldeffoo(args):passk=timetester.timeTester(foo)k.runtests()Lets say the return value of k (k.__repr__()) is 0.1k==0.1# True(float)k<1# True(int)k>=Decimal(0.0001)# True(Decimal object)k==0.002# False(float)k>1# False(int)k<=Decimal(0.00001)# False(Decimal object)Compare object optionsWhen creating the object,  the arguments must all be functionsVariables can only be changed laterSo let's say this is the initial code:importtimetesterdeffoo():passdefbar():passk=timetester.compare(foo,bar)looptime(int)Changes how many 'loops' would be ran everytime compareFuncs() would return# Defaultk.looptime>>2# Changek.looptime=10setattr(k,'looptime',10)# Restartk.initialise()runtime(int)Acts like timeTester.runtime option# Defaultk.runtime>>50# Changek.runtime=100setattr(k,'looptime',100)# Restartk.initialise()errortime(int,float, decimal.Decimal object)Acts as timetester.error_time option# Defaultk.errortime>>0# off# Changek.errortime=0.0001setattr(k,'errortime',0.0001)# Restartk.initialise()print(bool)Acts as the timetester.print_output option# Defaultk.print>>False# Changek.print=Falsesetattr(k,'print',False)# Restartk.initialise()meantype(string)Acts as the timetester.type option# Defaultk.meantype>>'harmonicmean'# Changek.meantype='median'setattr(k,'meantype','median')# Restartk.initialise()