Yet Another Python Profiler (Yappi)
Yappi (Yet Another Python Profiler) is a fast, C-implemented tracing profiler for Python applications, notably supporting multithreaded, asyncio, and gevent code. It overcomes limitations of standard profilers like `cProfile` by offering per-thread CPU time analysis, on-the-fly profiling capabilities, and flexible mechanisms for filtering and sorting profiling results. The library is currently at version 1.7.6 and maintains an active release schedule.
Warnings
- breaking Older versions of Yappi (pre-1.x) supported Python 2.6.x up to 3.4. Recent versions (from 1.7.0 onwards) officially support Python 3.6 and higher, including Python 3.14. Users on older Python 2 or early Python 3 versions will need to use an older Yappi release or upgrade their Python interpreter.
- gotcha Yappi operates as a per-interpreter resource. Attempting to run multiple Yappi profiler instances within the same Python interpreter concurrently will raise an exception.
- gotcha When interpreting profiling results, distinguish between 'tottime' and 'tsub'. 'tottime' (total time) includes time spent in sub-functions called from the profiled function, while 'tsub' (subtime) represents time spent directly within the function itself, excluding its sub-calls.
- gotcha By default, Yappi profiles using 'CPU time'. For I/O-bound or concurrent applications (e.g., asyncio, gevent), 'Wall time' might be more relevant to capture actual elapsed time, including waits.
Install
-
pip install yappi
Imports
- yappi
import yappi
Quickstart
import yappi
import time
def calculate_sum(n):
total = 0
for i in range(n):
total += i
return total
def main_task():
time.sleep(0.1) # Simulate some I/O or other work
result = calculate_sum(1_000_000)
time.sleep(0.05)
return result
# Start profiling
yappi.set_clock_type("cpu") # Can be "wall" for wall time
yappi.start()
# Run the code to be profiled
main_task()
# Stop profiling and print statistics
yappi.stop()
# Get function statistics and print them
func_stats = yappi.get_func_stats()
print("Function Statistics:")
func_stats.print_all(
columns={0:('ncall', 5), 1:('tottime', 8), 2:('avgtime', 8), 3:('file', 30)},
sort_type='tottime'
)
# Clear collected statistics
yappi.clear_stats()