Monday, July 4, 2022
HomeWordPress DevelopmentCode Benchmarking in Python utilizing Coach ⏱

Code Benchmarking in Python utilizing Coach ⏱


Have you ever ever needed to benchmark components of your code however discovered that almost all libraries prohibit you to the perform scope degree measuring (and nothing extra granular)?

No? Nicely, we did at my office. To unravel the difficulty, we leveraged the facility of Python contexts. Try the next instance to grasp what I imply:

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

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

# ... later within the code

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

Exit fullscreen mode

prints:

{
    'metric1': {'begin': 1656844808.09, 'finish': 1656844808.59, 'interval': 0.5},
    'metric2': {'begin': 1656844808.59, 'finish': 1656844808.89, 'interval': 0.3},
    'whole': {'begin': 1656844808.09, 'finish': 1656844808.89, 'interval': 0.8}
} 
Enter fullscreen mode

Exit fullscreen mode

Utilizing Python contexts (the “with” statements), your benchmarking scope is just not restricted to entire features (on this case, example_func), however you’ll be able to indent particular components of your code you’d prefer to measure.

It’s a quick however intelligent piece of code we name Coach ⏱ that’s now obtainable to anybody on my GitHub and might be put in as a package deal utilizing pip. Be at liberty to go away your star as you would possibly want this sooner or later.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments