Skip to content
Jacob Morris edited this page Sep 4, 2017 · 31 revisions

pytimer.py

Table of Contents

  • pytimer.PyTimer
  • PyTimer is a class for easily timing execution of sections of codes. PyTimer supports splitting to allow different segments of code to be timed separately.

    • Constants

    • Static Methods

      • Allows the elapsed time to be easily checked using repeated calls to this. There must be an initial call that gets the start time.

        • display (optional): whether to display the elapsed time, or to return it as a value
        • message (optional): if display==True, this message will be attached with the output that is displayed
        • return: the elapsed time if display==False
      • A static method that allows timing a function or string of code without creating a PyTimer object

        • block: either a callable, or a string
        • args: any positional arguments to be passed into block if it is a function
        • reps (optional): the number of times to average the elapsed time
        • runs_per_rep (optional): the number of runs for every rep. Useful if block is generally called multiple times
        • display (optional): if True, then display the calculated time, else, return the value
        • message (optional): if display==True, then this message will be displayed with the calculated value
        • callable_args (optional): whether to look through args and kwargs and see if any parameters are callable. If so, replace their value with the value returned from calling them. Only works if block is callable.
        • kwargs: any keyword arguments to be passed into block if it is a function
        • return: If block is not callable or a string, then None is returned. If display==True, then nothing is returned, else if display==False, the calculated time is returned
    • Methods

      • Calculates average for split i

        • i: Index of split to determine average for
        • return: None if empty split, False if invalid index, otherwise average for split
      • Returns list of averages for every split

        • return: list of averages, if position i is invalid, then list[i] is None
      • Function used as a decorator for timing functions. Decorator can have arguments, which allows the reps, runs_per_rep and whether the args are callable to be set for each function this is being used on. To use: @timer.decorator(reps=10, callable_args=True), where timer is a PyTimer instance.

        • reps (optional): the number of times to average the elapsed time
        • runs_per_rep (optional): the number of runs for every rep. Useful if block is generally called multiple times
        • callable_args (optional): whether to look through args and kwargs that are being passed to the function being decorated and see if any parameters are callable. If so, replace their value with the value returned from calling them.
        • return: a function that takes the function being decorated as a parameter
      • Calculates standard deviation for split i

        • i: split index
        • return: None if split i is empty, False if invalid index, otherwise returns standard deviation of split i
      • Calculates standard deviations for every split

        • return: list of standard deviations for all splits. If split i is empty, than list[i] is None
      • Display average for split i if valid index and split is not empty, otherwise displays appropriate message

        • i: split index
      • Display averages for all splits unless split i is empty, in which case it is skipped

      • Display standard deviation for split i if valid index and split is not empty, otherwise displays appropriate message

        • i: split index
      • Display standard deviation for all splits unless split is empty, in which case that split is skipped

      • Display time since last start() or reset(). This time includes that time taken to execute PyTimer method calls

      • Display all values in split i if valid index and split is not empty, otherwise displays appropriate message

        • i: split index
      • Display all values for all splits unless split is empty, in which case the split is skipped

      • Log elapsed time, log will have no affect if timer is paused

        • message (optional): optional: store message with the log
        • return: Elapsed time since start() in seconds, includes time to execute PyTimer method calls
      • Pause the timer, meaning no actions can be performed, allows portions of code to be skipped

      • Clear the timer back to its defaults

      • Resume the timer from being paused, updates the internal clock to the current time

      • Times a string of code or a function and times how long it takes for each iteration. If block is a function, then parameters can be passed to it like so: time(bar, "something", 12, runs_per_rep=100) -> bar("something", 12). No error checking is done, meaning any error that is raised within block will crash the entire program.

        • block: either function or string of code
        • args: any arguments that needs to be passed into block if block is a function
        • reps (optional): the number of times to average the elapsed time
        • runs_per_rep (optional): the number of runs for every rep. Useful if block is generally called multiple times
        • split_message (optional): the message that will be recorded with this split
        • callable_args (optional): whether to look through args and kwargs and see if any parameters are callable. If so, replace their value with the value returned from calling them. Only works if block is callable.
        • kwargs: keyword arguments that will be passed into block if it is a function
      • Return list of elapsed times for split i

        • i: index of split
        • return: returns list of elapsed times for split i if valid index, otherwise returns None
      • Write all collected output to a file, assuming that _collect_output was set in constructor. File is either written to folder_path + basename if file_per_run is false or folder_path + basename + date_time where date_time is formatted "-day-month-year-hours-minutes-seconds". All files are written to .txt format

        • folder_path: Path of folder to write to
        • basename: Name of file
        • file_per_run (optional): Create a new file with every run

Clone this wiki locally