Yet Another Python Profiler (Yappi)

1.7.6 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates how to profile a simple Python function using `yappi.start()`, executing the target code, `yappi.stop()`, and then printing function statistics. It also shows how to set the clock type (CPU or Wall time) and clear the collected stats.

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()

view raw JSON →