# Benchmarking a value dependent function

**URL:** <https://discourse.julialang.org/t/benchmarking-a-value-dependent-function/137683>\
**Category:** General Usage\
**Tags:** question, benchmarks\
**Created:** [June 18, 2026, 10:47am UTC](https://discourse.julialang.org/t/benchmarking-a-value-dependent-function/137683 "2026-06-18T10:47:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 18, 2026, 10:47am UTC](https://discourse.julialang.org/t/benchmarking-a-value-dependent-function/137683/1 "2026-06-18T10:47:55Z")

</div>

I have a package that, at its core, performs a calculation `f(P, x)` for various values of `x`, using various user-defined methods for problem `P` and inputs `x`.

The key is that even though all the components are type-stable, the _value_ of `x` will affect the runtime (not unlike a parametric rootfinding problem will have a harder time finding a root in some locations). But at the same time, learning about the Julia-specific breakdown of runtime (eg gc stats) would benefit the user.

What the user should get out of this is a summary (eg quantiles) for runtimes, gc times, lock conflicts, etc.

Is `@timed` the best entry point for this, in a setting not unlike

```julia
do_timed(f, M, N = 1000) = [(x = randn(M); @timed(f(x))) for _ in 1:N]

```

?

Since the macro is inside the function, is it correct to assume that if `f` is type-stable, `compile_time` and `recompile_time` will be 0?

Or should I directly invoke `time_ns()` and `Base.gc_num()` like [BenchmarkTools](https://github.com/JuliaCI/BenchmarkTools.jl/blob/4258940e99ef9ac44a3a1dfec1dfd924a9f4a4a4/src/execution.jl#L657)?

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [June 18, 2026, 11:11am UTC](https://discourse.julialang.org/t/benchmarking-a-value-dependent-function/137683/2 "2026-06-18T11:11:06Z")

</div>

It really depends on what you actually want to show. `@timed` is likely fine for your purposes, I’d probably prefer it over `time_ns` and `Base.gc_num`.

> [@Tamas\_Papp](#):
>
> Since the macro is inside the function, is it correct to assume that if `f` is type-stable, `compile_time` and `recompile_time` will be 0?

Technically, `f` itself being type stable might not be enough, since you can have localized instabilities that are “recovered” inside a type stable function. But yeah, that’s more or less right.

One thing to note here, is that `@benchmark` from BenchmarkTools.jl is often quite good at giving an overview of the stats you’re talking about (though not lock conflicts):

```julia-auto
julia> @benchmark sum(rand(N, N)^2) setup=(N = rand(1:100))
BenchmarkTools.Trial: 306 samples with 986 evaluations per sample.
 Range (min … max): 72.999 ns … 92.887 μs ┊ GC (min … max): 0.00% … 51.74%
 Time (median): 8.139 μs ┊ GC (median): 4.97%
 Time (mean ± σ): 16.637 μs ± 18.097 μs ┊ GC (mean ± σ): 7.11% ± 7.87%

  █▂                                                           
  ██▇██▅▄▅▃▃▄▄▂▃▃▃▅▃▃▄▁▁▃▃▃▃▂▃▃▂▃▂▁▁▂▂▃▃▄▃▂▄▃▃▃▃▄▃▄▃▁▂▁▁▁▂▁▁▁ ▃
  73 ns Histogram: frequency by time 60.7 μs <

 Memory estimate: 224 bytes, allocs estimate: 4.

```
