aerosandbox.tools.code_benchmarking =================================== .. py:module:: aerosandbox.tools.code_benchmarking Classes ------- .. autoapisummary:: aerosandbox.tools.code_benchmarking.Timer Functions --------- .. autoapisummary:: aerosandbox.tools.code_benchmarking.time_function aerosandbox.tools.code_benchmarking.f Module Contents --------------- .. py:class:: Timer(name = None, verbose = True) Bases: :py:obj:`object` A context manager for timing things. Use it like this: with Timer("My timer"): # You can optionally give it a name # Do stuff Results are printed to stdout. You can access the runtime (in seconds) directly by instantiating the object: >>> t = Timer("My timer") >>> t.tic() >>> # Do stuff >>> print(t.toc()) Nested timers are also supported. For example, this code: >>> with Timer("a"): >>> with Timer("b"): >>> with Timer("c"): >>> f() prints the following console output: [a] Timing... [b] Timing... [c] Timing... [c] Elapsed: 100 msec [b] Elapsed: 100 msec [a] Elapsed: 100 msec .. py:attribute:: number_running :type: int :value: 0 .. py:attribute:: name :type: str :value: None .. py:attribute:: verbose :type: bool :value: True .. py:attribute:: runtime :type: float .. py:method:: __repr__() .. py:method:: _format_time(time_seconds) :staticmethod: .. py:method:: _print(s, number_running_mod = 0) .. py:method:: tic() .. py:method:: __enter__() .. py:method:: toc() .. py:method:: __exit__(type, value, traceback) .. py:function:: time_function(func, repeats = None, desired_runtime = None, runtime_reduction=np.min, warmup = True) Runs a given callable and tells you how long it took to run, in seconds. Also returns the result of the function (if any), for good measure. :param func: The function to run. Should take no arguments; use a lambda function or functools.partial if you need to pass arguments. :param repeats: The number of times to run the function. If None, runs until desired_runtime is met. :param desired_runtime: The desired runtime of the function, in seconds. If None, runs until repeats is met. :param runtime_reduction: A function that takes in a list of runtimes and returns a reduced value. For example, np.min will return the minimum runtime, np.mean will return the mean runtime, etc. Default is np.min. :param warmup: If True, runs the function once before starting the timer. This can be useful for functions with JIT-compilation, with internal imports, or caches. Returns: A Tuple of (time_taken, result). - time_taken is a float of the time taken to run the function, in seconds. - result is the result of the function, if any. .. py:function:: f()