# Comparing benchmarks

**URL:** <https://discourse.julialang.org/t/comparing-benchmarks/8901>\
**Category:** New to Julia\
**Tags:** benchmarktools\
**Created:** [February 7, 2018, 5:48pm UTC](https://discourse.julialang.org/t/comparing-benchmarks/8901 "2018-02-07T17:48:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)\
**Post date:** [February 7, 2018, 5:48pm UTC](https://discourse.julialang.org/t/comparing-benchmarks/8901/1 "2018-02-07T17:48:49Z")

</div>

Assume two functions f and g whose relative performance  
I would like to determine.

```
@time f(100) -> f_sec (f_alloc: f_mem)
@time g(100) -> g_sec (f_alloc: f_mem)

```

I would like to have a macro which gives me

```
@relperf f(100), g(100) -> (f_sec/g_sec, f_alloc/g_alloc, f_mem/g_mem)

```

It is beyond my knowledge to write such a macro.  
Maybe someone is willing to give it away?

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [February 7, 2018, 7:51pm UTC](https://discourse.julialang.org/t/comparing-benchmarks/8901/2 "2018-02-07T19:51:59Z")

</div>

[BenchmarkTools.jl](https://github.com/JuliaCI/BenchmarkTools.jl/blob/master/doc/manual.md#trialratio-and-trialjudgement)!  
The link points to the `ratio` and `judge` functions in the docs.

---

<div class="post-metadata">

**Author:** ![JackDevine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jackdevine/32/1048_2.png) [@JackDevine](https://discourse.julialang.org/u/JackDevine)\
**Post date:** [February 7, 2018, 7:53pm UTC](https://discourse.julialang.org/t/comparing-benchmarks/8901/3 "2018-02-07T19:53:17Z")

</div>

Have you seen the `@timed` macro?

```julia
help?> @timed
  @timed

  A macro to execute an expression, and return the value of the expression,
  elapsed time, total bytes allocated, garbage collection time, and an object
  with various memory allocation counters.

  See also @time, @timev, @elapsed, and @allocated.

  julia> val, t, bytes, gctime, memallocs = @timed rand(10^6);
  
  julia> t
  0.006634834
  
  julia> bytes
  8000256
  
  julia> gctime
  0.0055765
  
  julia> fieldnames(typeof(memallocs))
  9-element Array{Symbol,1}:
   :allocd
   :malloc
   :realloc
   :poolalloc
   :bigalloc
   :freecall
   :total_time
   :pause
   :full_sweep
  
  julia> memallocs.total_time
  5576500

```

**Edit** I just saw what Elrod wrote, lol. Definitely use BenchmarkTools for more accurate benchmarks, because they deal with warm up time and they average times as well.
