# PerfTimer - code timer with Trio async support

**URL:** <https://trio.discourse.group/t/perftimer-code-timer-with-trio-async-support/204>\
**Category:** Announcements\
**Created:** [August 14, 2019, 11:50pm UTC](https://trio.discourse.group/t/perftimer-code-timer-with-trio-async-support/204 "2019-08-14T23:50:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![belm0](https://yyz2.discourse-cdn.com/free1/user_avatar/trio.discourse.group/belm0/32/69_2.png) [@belm0](https://trio.discourse.group/u/belm0)\
**Post date:** [August 14, 2019, 11:50pm UTC](https://trio.discourse.group/t/perftimer-code-timer-with-trio-async-support/204/1 "2019-08-14T23:50:17Z")

</div>

PerfTimer seems like yet another code execution timer-- but wait! The package also includes `TrioPerfTimer`, which is aware of the Trio scheduler and able to deduct periods when a task is blocked. So you can measure the CPU time of any section of code including async calls.

If you’re not looking for a fancy context-manager timer thing, the package also has a `trio_perf_counter()` primitive, which follows the semantics of the stdlib `time` perf counters but in the context of a Trio task. Use it to build your own instrumentation stuff!

> **[belm0/perf-timer](https://github.com/belm0/perf-timer)**
>
> An indispensable performance timer for Python. Contribute to belm0/perf-timer development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![belm0](https://yyz2.discourse-cdn.com/free1/user_avatar/trio.discourse.group/belm0/32/69_2.png) [@belm0](https://trio.discourse.group/u/belm0)\
**Post date:** [July 2, 2020, 11:39pm UTC](https://trio.discourse.group/t/perftimer-code-timer-with-trio-async-support/204/2 "2020-07-02T23:39:58Z")

</div>

perf-timer has recently been updated:

- now tracks standard deviation by default
- percentile tracking is available `PerfTimer(..., observer=HistogramObserver)`, which can report arbitrary percentile latencies
- overhead of the various observer and timer types has been benchmarked. On an old laptop, the default `PerfTimer` takes \< 2 µs per observation, and `TrioPerfTimer` takes \< 5 µs.

# example output

default (avg ± stddev, max):

```nohighlight
timer "process thumbnail": avg 73.1 µs ± 18.0 µs, max 320.5 µs in 292 runs

```

histogram (avg ± stddev, percentiles):

```nohighlight
timer "test": avg 117ms ± 128ms, 50% ≤ 81.9ms, 90% ≤ 243ms in 50 runs

```
