# Equivalent of BenchmarkTools in Python?

**URL:** https://discourse.julialang.org/t/equivalent-of-benchmarktools-in-python/97011
**Category:** Offtopic
**Tags:** question, benchmark, benchmarktools, python
**Created:** [April 3, 2023, 1:01pm UTC](https://discourse.julialang.org/t/equivalent-of-benchmarktools-in-python/97011 "2023-04-03T13:01:45Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [April 3, 2023, 1:01pm UTC](https://discourse.julialang.org/t/equivalent-of-benchmarktools-in-python/97011/1 "2023-04-03T13:01:45Z")

</div>

Which is the equivalent of @btime/ @benchmark in Python?  
I would like something to interactively benchmark a single expression (function call) and where the number of run adapt to the running cost of the expression to benchmark.

I can’t get timeit/pyperf for this ☹

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [April 3, 2023, 1:06pm UTC](https://discourse.julialang.org/t/equivalent-of-benchmarktools-in-python/97011/2 "2023-04-03T13:06:35Z")

</div>

> **[GitHub - JuliaCI/BenchmarkTools.jl: A benchmarking framework for the Julia...](https://github.com/JuliaCI/BenchmarkTools.jl)**
>
> A benchmarking framework for the Julia language. Contribute to JuliaCI/BenchmarkTools.jl development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [April 3, 2023, 1:08pm UTC](https://discourse.julialang.org/t/equivalent-of-benchmarktools-in-python/97011/3 "2023-04-03T13:08:56Z")

</div>

yes, but I am looking to something similar in Python…

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [April 3, 2023, 1:11pm UTC](https://discourse.julialang.org/t/equivalent-of-benchmarktools-in-python/97011/4 "2023-04-03T13:11:38Z")

</div>

omg sorry… I guess it’s time for a break…

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [April 3, 2023, 1:33pm UTC](https://discourse.julialang.org/t/equivalent-of-benchmarktools-in-python/97011/5 "2023-04-03T13:33:55Z")

</div>

It’s crazy how little there is about this in Python… I was convinced to find something like this in seconds, instead the best out there seems to get the current time before and after and subtract, or profile a whole script using command line tools…  
Th beauty of @BenchmarkTools is that it is so simple, just `@btime ...code..` and you are done, no need to create a struct for the number of times to run the code, the name of the benchmark… sometimes it’s ok, but most of the time I just need a quick benchmark, and that’s what BenchmarkTools provides, but crazy I can’t find something similar in Python…

Crosspost on SO: [benchmarking - How to benchmark a single function call in Python? - Stack Overflow](https://stackoverflow.com/questions/75919967/how-to-benchmark-a-single-function-call-in-python)  
(end of course, starting having downvotes there, I don’t know why, as I did search before…)

---

<div class="post-metadata">

### Author: ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)
#### Post date: [April 3, 2023, 1:43pm UTC](https://discourse.julialang.org/t/equivalent-of-benchmarktools-in-python/97011/6 "2023-04-03T13:43:36Z")

</div>

there is [GitHub - plasma-umass/scalene: Scalene: a high-performance, high-precision CPU, GPU, and memory profiler for Python with AI-powered optimization proposals](https://github.com/plasma-umass/scalene), but I’ve never used it myself before

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [April 3, 2023, 1:47pm UTC](https://discourse.julialang.org/t/equivalent-of-benchmarktools-in-python/97011/7 "2023-04-03T13:47:20Z")

</div>

Looking at the docs for timeit, it does seem like you can get it to behave as `@benchmark` does.

> **[timeit — Measure execution time of small code snippets](https://docs.python.org/3/library/timeit.html)**
>
> Source code: Lib/timeit.py This module provides a simple way to time small bits of Python code. It has both a Command-Line Interface as well as a callable one. It avoids a number of common traps fo...

See `autorange`, for example.

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [April 3, 2023, 2:17pm UTC](https://discourse.julialang.org/t/equivalent-of-benchmarktools-in-python/97011/8 "2023-04-03T14:17:00Z")

</div>

We’ve been slowly setting up cross-language benchmarks. I agree the Python options are slightly weird, but you can see what we ended up with here: [image\_benchmarks/benchmarks.py at main · JuliaImages/image\_benchmarks · GitHub](https://github.com/JuliaImages/image_benchmarks/blob/main/python-skimage/benchmarks.py)

Surprising that even simple stuff like dividing by the number of repeats appears to be something you have to do manually.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [April 3, 2023, 3:09pm UTC](https://discourse.julialang.org/t/equivalent-of-benchmarktools-in-python/97011/9 "2023-04-03T15:09:42Z")

</div>

If using `IPython` the `%timeit` macro is probably the closest you can get.
