Skip to content
Jacob Morris edited this page May 21, 2017 · 31 revisions

Class Attributes

  • seconds -> abbreviated s
  • milliseconds -> abbreviated ms (10-3 s)
  • microseconds -> typically abbreviation is μs, but to avoid UNICODE issues, it is abbreviated us (10-6 s)
  • nanoseconds -> abbreviated ns (10-9 s)
  • rounding -> the number of digits to round displayed numbers to

Static Methods

  • elapsed([display=True, message=""]): None | float -> Easily check how much time has elapsed since the last call to this method. This requires that there is an initial call. The elapsed time can be displayed, optionally with a message, or the value can be returned.

  • display=True: bool -> Display the resulting elapsed time, or return the value

  • message="": str -> Add a message to the displayed result, will only happen if display=True

  • time_it(block, *args, [reps=1, iterations=10, display=True, message=""], **kwargs): None | float -> This method is the static version of time and allows callable objects and strings to be timed. If block is callable, then all args and kwargs (except reps and iterations) will be passed into block when it is called. Function returns the average time of all the iterations.

  • block: str | callable -> The function or string of code to be timed

  • args: varied [optional] -> Any positional arguments to pass into block if it is callable

  • reps=1: int-> The number of repititions to do within each iteration

  • iterations=10: int -> The number of iterations to time and average together

  • display=True: bool -> Display the averaged time, or return the value

  • message="": str -> If display, then attach this message when displaying the time

Constructor

  • PyTimer(start=True, [round=4, units=None, run=True, collect=False, display=True])
  • start=True: bool -> Automatically run start() when creating object, thus start recording the time here
  • round=4: int -> Number of digits to round values to
  • run=True: bool -> Do functions calls to timer object do anything, useful for disabling timer without removing code
  • collect=False: bool -> Collect all data the is, or would be, displayed
  • display=True: bool -> Whether or not to display any output
  • units=None: str -> Units to display times in, must be in (PyTimer.seconds, PyTimer.milliseconds, PyTimer.microseconds, PyTimer.nanoseconds), if None, then all values will be automatically converted to closest unit

Methods

  • average(i): int -> Returns average of i'th split, or None if empty split or False if i is an invalid index

  • i: int -> Index of split to determine average for

  • averages(): list -> Returns list of averages for every split, if position i is empty, then list[i] is None

  • decorator(func): callable -> For use only as a decorator like @timer.decorator, this allows the function to be timed when it is called. WARNING: Do not use decorator with recursive function as this will lead to a spiral of calls within the decorator. Instead, use the evaluate() method.

  • func: callable -> Function to use decorator on

  • deviation(i): float -> Returns the standard deviation of split i or None if i is empty split otherwise False if i is an invalid index

  • i: int -> Index of split to determine standard deviation for

  • deviations(): list -> Returns list of standard deviations for every split, if split i is empty then list[i] is None

  • display_average(i) -> Display average of split i if valid index, if empty split or invalid index than an appropriate messages is displayed

  • i: int -> index of split to display average for

  • display_averages() -> Display averages for every split, if split is empty than it is skipped

  • display_deviation(i) -> Display standard deviation for split i, if empty split or invalid index than an appropriate message is displayed

  • i: int -> index of split to display standard deviation for

  • display_deviations() -> Display standard deviations for all splits, if split is empty than it is skipped

  • display_split(i) -> Display all logged values in split i, if split is empty or invalid index than an appropriate message is displayed

  • i: int -> index of split to display logged values for

  • display_splits() -> Display all logged values for all splits, if split is empty than it is skipped

  • log(message="") -> Log time since start, last log, last split, or last resume, depending on which happened most recently

  • message="": str -> message to attach to log

  • overall_time(): float -> time elapsed since start() or reset() in seconds. This includes time spend executing PyTimer calls.

  • pause() -> pause timer

  • reset() -> clear all recorded values, call start() to update internal time values

  • resume() -> start back from pause(), update internal time values

  • setup_decorator([reps=1, iterations=10]) -> allow number of reps and iterations to be set for decorator

  • reps=1: int -> number of reps for decorator

  • iterations=10: int -> number of iterations for decorator

  • split(message="") -> Create separate group of logs

  • message="": str -> message to be attached to split, used as name when displaying anything associated with this split

  • start() -> setup internal time values, must be called manually if start is set to False when object was created

  • time(block, *args, [reps=1, iterations=10, message=""], **kwargs) -> Runs block reps times, then calls log() and repeats iterations times. split() is called at end with message based on text of block, or the name of the function, or message if specified

  • block: str | callable -> string block of code, or a function to be timed

  • args: varied [optional] -> arguments to be passed into block if it is a function

  • reps=1: int -> number of reps, defaults to 10

  • iterations=10: int -> number of iterations, defaults to 10

  • message="": str -> message to use for split, if not specified, then the message is block.__name__ if block is a function. Otherwise, the message is block if it is a str

  • kwargs: varied [optional]-> keyword arguments, all but the three listed above will be passed directly into block if it is callable


  • times(i): list -> returns list of elapsed times for split i or None if invalid index

  • i: int -> index for split w/ elapsed times

  • write_output(fp) -> will write all collected output to file path fp if collect was set to True when creating object, otherwise an appropriate message will be displayed. PermissionError will be thrown if file path cannot be written to.

  • fp: str -> file path to write file to

Clone this wiki locally