Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Julia equivalent of Python's %timeit

In Python, a convenient way to evaluate the performance of code is timeit. In Julia we have @time, but this has the disadvantage of only running a piece of code once, which means you need to execute it several times to get a good picture of code performance. Is there a better way to time code in Julia, more akin to Python's timeit?

like image 561
Yly Avatar asked Feb 18 '18 01:02

Yly


1 Answers

The package BenchmarkTools has @benchmark and @btime which has a statistical way of determining the number of runs.

julia> A = rand(100,100);
julia> B = rand(100,100);

julia> using BenchmarkTools

julia> @benchmark A*B
BenchmarkTools.Trial:
  memory estimate:  78.20 KiB
  allocs estimate:  2
  --------------
  minimum time:     48.302 μs (0.00% GC)
  median time:      72.015 μs (0.00% GC)
  mean time:        74.314 μs (6.52% GC)
  maximum time:     3.232 ms (95.17% GC)
  --------------
  samples:          10000
  evals/sample:     1

julia> @btime A*B;
  49.180 μs (2 allocations: 78.20 KiB)
like image 148
Chris Rackauckas Avatar answered Nov 16 '22 08:11

Chris Rackauckas