Inline Code logo pyinstrument: inline code

1 2 3 4 5 6 7

Pyinstrument can be customised by including it explicitly in your Python scripts. This will give you more control on what to measure. The general syntax for this is shown below.

from pyinstrument import Profiler

profiler = Profiler()
profiler.start()

# This is where your custom code goes.

profiler.stop()
profiler.print()

An Example

The code below demonstrates a runnable example.

import time
from pyinstrument import Profiler

profiler = Profiler()
profiler.start()

sleep_time = 0.1

def start():
    time.sleep(sleep_time)
    do_sleep1()
    do_sleep2()

def do_sleep1():
    time.sleep(sleep_time)

def do_sleep2():
    do_sleep1()
    time.sleep(sleep_time)

start()

profiler.stop()
profiler.print()

Custom

Instead of running profiler.print() you can also run profiler.open_in_browser() to generate the familiar html report shown in a previous video.