DEV Community

mm
mm

Posted on • Edited on

3 2

Code Benchmarking in Python using Trainer ⏱

Have you ever wanted to benchmark parts of your code but found that most libraries restrict you to the function scope level measuring (and nothing more granular)?

We did at my workplace. To solve the issue, we leveraged the power of Python contexts. Check out the following example to understand what I mean:

def example_func():
    with t('metric1'):
        time.sleep(0.5)

    with t('metric2'):
        time.sleep(0.3)

# ... later in the code

m = t.add_total('total').metrics
print(m)
Enter fullscreen mode Exit fullscreen mode

prints:

{
    'metric1': {'start': 1656844808.09, 'end': 1656844808.59, 'interval': 0.5},
    'metric2': {'start': 1656844808.59, 'end': 1656844808.89, 'interval': 0.3},
    'total': {'start': 1656844808.09, 'end': 1656844808.89, 'interval': 0.8}
} 
Enter fullscreen mode Exit fullscreen mode

Using Python contexts (the "with" statements), your benchmarking scope is not limited to whole functions (in this case, example_func), but you can indent specific parts of your code you'd like to measure.

It is a short but clever piece of code I call Trainer ⏱ that is now available to anyone on my GitHub and can be installed as a package using pip. Feel free to leave your star as you might need this in the future.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (1)

Collapse
 
farrukh007 profile image
Farrukh

Hi...do u have experienced in Machine learning programming?
I need to trained my dataset and get results over multiple supervised Machine learning algorithms.
Can u help me in the above context?

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay