profiling

Sets up a logging framework that can be imported and used anywhere.

Module summary

Classes

Profiler([distribution, track_memory, …])

Performance profiler class.

Functions

add_logging_level(num, name)

Adds a custom logging level to all loggers.

change_logfile(logger, path)

Deletes any previous FileHandler handlers and creates a new FileHandler to the new path with the same level as the previous one.

file_level(logger, level)

Set the FileHandler level.

get_logger([name, path, file_level, term_level])

Creates a pre-configured logger.

get_logger_formats()

term_level(logger, level)

Set the StreamHandler level.

Contents

Profiler

class sorts.profiling.Profiler(distribution=False, track_memory=False, snapshot_total=True)[source]

Bases: object

Performance profiler class.

The named performance control is especially useful for timing contents of loops.

Usage example:

p = Profiler()
p.start('list init')
p.start('program')
lst = list(range(200))
p.stop('list init')
for i in range(1000):
    p.start('list reversal')
    lst = lst[::-1]
    p.stop('list reversal')
p.stop('program')

print(p)
fmt(normalize=None, timedelta=False)[source]

Format summary of the profiler into a string.

Parameters
  • normalize (str) – Name of the profiling location to normalize execution time towards.

  • timedelta (bool) – Print execution times as time-deltas.

memory_diff(name, save=None)[source]

Calculate a memory difference between the latest snapshot and now.

Parameters
  • name (str) – Name of the profiling location.

  • save (str) – Save memory difference to this name, default is same as name.

snapshot(name)[source]

Take a tracemalloc snapshot.

Parameters

name (str) – Name of the profiling location.

start(name)[source]

Records a start time for named call.

Parameters

name (str) – Name of the profiling location.

stop(name)[source]

Records a time elapsed since the latest start time for named call.

Parameters

name (str) – Name of the profiling location.

Functions

sorts.profiling.add_logging_level(num, name)[source]

Adds a custom logging level to all loggers.

Parameters
  • num (int) – Integer level for logging level.

  • name (str) – Name of logging level.

sorts.profiling.change_logfile(logger, path)[source]

Deletes any previous FileHandler handlers and creates a new FileHandler to the new path with the same level as the previous one.

Parameters
  • logger (logging.Logger) – Logger object.

  • path (str/pathlib.Path) – Path to the logfile output. Can be a file or folder.

  • file_level (int) – Logging level of the file handler.

  • term_level (int) – Logging level of the terminal handler.

sorts.profiling.file_level(logger, level)[source]

Set the FileHandler level.

sorts.profiling.get_logger(name='sorts', path=None, file_level=20, term_level=20)[source]

Creates a pre-configured logger. Formats to output both to terminal and a log file.

Note: Can only use folders as paths for MPI usage.

Parameters
  • name (str) – Name of the logger.

  • path (str/pathlib.Path) – Path to the logfile output. Can be a file, a folder or None.

  • file_level (int) – Logging level of the file handler.

  • term_level (int) – Logging level of the terminal handler.

sorts.profiling.get_logger_formats()[source]
sorts.profiling.term_level(logger, level)[source]

Set the StreamHandler level.