Python profile Module: Usage and cProfile Comparison
The profile module records call counts and timings to find slow code. See how it compares with cProfile, how to read reports and use pstats.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
The profile module records call counts and timings to find slow code. See how it compares with cProfile, how to read reports and use pstats.
cProfile is Python's built-in profiler that shows how often each function runs and where time is spent. Learn to run it and read the results with pstats.
The pstats module turns raw cProfile output into sorted, filtered reports so you can find the functions that slow your Python program down.
pdb is Python's built-in interactive debugger. Learn breakpoint(), the key commands, conditional breakpoints and post-mortem debugging with examples.
doctest runs the interactive examples in your docstrings and verifies their output, so documentation doubles as tests. Learn usage, flags and pytest integration.
Python's unittest module is the built-in framework for writing and running automated tests. See test cases, assertions, fixtures, mocking and test discovery.
The Python logging module records events with severity levels, handlers and formatters. Learn setup for scripts and apps, rotation, exception logging and best practices.
argparse builds command-line interfaces in Python from declared arguments, flags and subcommands. Learn it with runnable examples and help output.
The Python sys module exposes the interpreter and its environment: arguments, streams, exit codes, module paths and version info, with runnable examples.
The os module gives Python portable access to files, directories, environment variables and process data. See the key functions with runnable examples.
Python's gc module exposes the cyclic garbage collector. Learn how to collect, tune thresholds, inspect statistics and debug memory leaks with practical examples.
The weakref module lets Python code refer to objects without keeping them alive. See weak references, WeakValueDictionary, finalize and cycle-free parent links.
Learn how Python's struct module converts between Python values and binary data, with format codes, byte order rules and file header examples.
The array module stores numbers of one C type compactly, with list-like methods and binary file I/O. See type codes, examples and how it compares to lists and NumPy.
The bisect module finds where a value belongs in a sorted list in logarithmic time and can insert it there. See bisect_left, bisect_right, insort and the key argument.
Learn how the Python heapq module works: heappush, heappop, heapify, nlargest, merge, priority queues with tuples and max-heaps with examples.
The queue module gives threads a safe way to pass work around. See FIFO, LIFO, priority and simple queues and a correct worker pool pattern.
The collections module adds specialised containers such as Counter, deque and defaultdict. See what each one does, with examples and a guide to choosing between them.
A dictionary owns key-value data; keys(), values() and items() return live views. Learn how they differ, set operations and common mistakes.
A set holds unique hashable elements, a dict maps unique keys to values. Learn operations, ordering, performance and how to choose between them.