Python package to get an average time of your function
Project description
timetester
Feed it a method and it tells you the average time.
Downloads
pip install timetester
or
python3 -m pip install timetester
Usage
- Timetester Object
import timetester
def foo(arg):
print(arg)
pass
k = timetester.timeTester(foo)
# NOT NECESSARY. Only use if you encountered an error and reloaded the object
k.initialise()
# Run tests
k.runtests(*args,**kwargs)
# Graph the average time taken
k.graph()
# Print a report of how the function did
k.report()
- Timeout Decorator
import timetester
import time
# Raises error
@timetester.timeout(10)
def foo(bar):
time.sleep(11)
- Compare object
The compared functions should have the same intended purpose and the same arguments
import timetester
def foo():
pass
def bar():
pass
k= timetester.compare(foo,bar)
# NOT NECESSARY. Only use if you encountered an error and reloaded the function
k.initialise()
# Arguments are passed in here
k.compareFuncs(*args,**kwargs)
# Outputs a graph comparing the time
k.graph()
# Creates a json file consising of the time took for each function to run
k.output_asfile()
# Prints each function on the screen
k.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 first
import timetester
def foo(args):
pass
def bar(args):
pass
# Allowed
timetester.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 first
timetester.timeTester(foo).graph().report()
timetester.timeTester(foo).report()
timetester.compare(foo,bar).initialise().graph().sort().output_asfile()
timetester.compare(foo,bar).graph().sort()
Default options
import timetester
def foo():
pass
def bar():
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 later
c = timetester.compare(foo,bar)
timeTester object options
- target(float/int)
The target time you want the function to be at
import timetester
def foo(arg):
pass
k = timetester.timeTester(foo,target=0.005)
# or
k.target =0.05
- runtime(int,float)
How many times the function is ran before taking average
import timetester
def foo(arg):
pass
k= timetester.timeTester(foo, runtime=100)
# or
k.runtime = 100
- print_output(bool)
disable all
print
function inside the provided function.
import timetester
def foo(arg):
print(arg)
k= timetester.timeTester(foo, print_output=False)
# or
k.print_output = False
# Even though the function is intended to print arguments, no output will be printed during run tests
k.runtests('hi','bye')
# After runtests print functions will be normal.
print('hi')
- maxtime(int/float)
Maximum time all functions would run
import timetester
def foo(arg):
sleep(1)
k=timetester.timeTester(foo,maxtime=1)
# or
k.maxtime = 1
k.runtests()
# When the total time is larger than 1 second error will be raised
- return_type(choice)
Type of value returned
Types of possible values include:
- 'mean'
- 'mode'
- 'median'
- 'harmonicmean'
- 'geometricmean'
import timetester
def foo(arg):
pass
k= timetester.timeTester(foo,return_type='geometricmean')
# or
k.type = 'geometricmean'
timeTester object compatibility
Allows comparision with int
, float
and decimal.Decimal
objects
Take this code below:
import timetester
from decimal import Decimal
def foo(args):
pass
k=timetester.timeTester(foo)
k.runtests()
Lets say the return value of k (k.__repr__()) is 0.1
k==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 options
- When creating the object, the arguments must all be functions
- Variables can only be changed later
So let's say this is the initial code:
import timetester
def foo():
pass
def bar():
pass
k=timetester.compare(foo,bar)
- looptime(int)
Changes how many 'loops' would be ran everytime compareFuncs() would return
# Default
k.looptime
>>2
# Change
k.looptime = 10
setattr(k, 'looptime',10)
# Restart
k.initialise()
- runtime(int)
Acts like timeTester.runtime option
# Default
k.runtime
>>50
# Change
k.runtime=100
setattr(k, 'looptime',100)
# Restart
k.initialise()
- errortime(int,float, decimal.Decimal object)
Acts as timetester.error_time option
# Default
k.errortime
>>0 # off
# Change
k.errortime = 0.0001
setattr(k,'errortime',0.0001)
# Restart
k.initialise()
- print(bool)
Acts as the timetester.print_output option
# Default
k.print
>>False
# Change
k.print = False
setattr(k, 'print',False)
# Restart
k.initialise()
- meantype(string)
Acts as the timetester.type option
# Default
k.meantype
>>'harmonicmean'
# Change
k.meantype = 'median'
setattr(k, 'meantype','median')
# Restart
k.initialise()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for timetester-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5afb5b80192a1f2cb48963f72eb8763445dbe0a13bd60c003b04ce2559316943 |
|
MD5 | 8a1038d29c591d9054911e70e8c9a8e0 |
|
BLAKE2b-256 | 0e76f758fedd1c8f24cf61b6fe233926df059241386ad76aeaa4861cdef9e4af |