Skip to content
Jacob Morris edited this page Jul 13, 2017 · 31 revisions

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.

    • Static Methods

      • elapsed(display=True, message="")

        • 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

      • time_it(block, *args, reps=1, iterations=10, display=True, message="", **kwargs)

        • 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 reps for each iteration

        • iterations (optional): the number of iterations to average together

        • 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

        • kwargs: any keyword arguments to be passed into block if it is a function

    • Methods

      • average(self, i)

        • Calculates average for split i

        • i: Index of split to determine average for

      • averages(self)

        • Returns list of averages for every split
      • decorator(self, func)

        • Returns a decorator so that functions can be timed with having to use evaluate, but functions can't take any parameters

        • func: takes a function as a parameter

      • deviation(self, i)

        • Calculates standard deviation for split i

        • i: split index

      • deviations(self)

        • Calculates standard deviations for every split
      • display_average(self, i)

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

        • i: split index

      • display_averages(self)

        • Display averages for all splits unless split i is empty, in which case it is skipped
      • display_deviation(self, i)

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

        • i: split index

      • display_deviations(self)

        • Display standard deviation for all splits unless split is empty, in which case that split is skipped
      • display_overall_time(self)

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

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

        • i: split index

      • display_splits(self)

        • Display all values for all splits unless split is empty, in which case the split is skipped
      • log(self, message="")

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

        • message (optional): optional: store message with the log

      • overall_time(self)

      • pause(self)

        • Pause the timer, meaning no actions can be performed, allows portions of code to be skipped
      • reset(self)

        • Clear the timer back to its defaults
      • resume(self)

        • Resume the timer from being paused, updates the internal clock to the current time
      • setup_decorator(self, reps=1, iterations=10)

        • Allow decorator to run function for multiple reps and iterations

        • reps (optional): number of times to run the function before logging the time

        • iterations (optional): number of times to calculate the time

      • split(self, message="")

        • message (optional):
      • start(self)

      • time(self, block, *args, reps=10, iterations=10, split_message="", **kwargs)

        • 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, iterations=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): number of reps to run the block before log is called

        • iterations (optional): number of times to run rep number of times

        • split_message (optional): the message that will be recorded with this split

        • kwargs: keyword arguments that will be passed into block if it is a function

      • times(self, i)

        • Return list of elapsed times for split i

        • i: index of split

      • write_output(self, folder_path, basename, file_per_run=False)

        • 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