julia# How to obtain the execution time of a function in Julia?

I want to obtain the execution time of a function in Julia. Here is a minimum working example:

```
function raise_to(n)
for i in 1:n
y = (1/7)^n
end
end
```

How to obtain the time it took to execute `raise_to(10)`

?

Solution

The recommended way to benchmark a function is to use `BenchmarkTools`

:

```
julia> function raise_to(n)
y = (1/7)^n
end
raise_to (generic function with 1 method)
julia> using BenchmarkTools
julia> @btime raise_to(10)
1.815 ns (0 allocations: 0 bytes)
```

Note that repeating the computation numerous times (like you did in your example) is a good idea to get more accurate measurements. But `BenchmarTools`

does it for you.

Also note that `BenchmarkTools`

avoids many pitfalls of merely using `@time`

. Most notably with `@time`

, you're likely to measure compilation time in addition to run time. This is why the first invocation of `@time`

often displays larger times/allocations:

```
# First invocation: the method gets compiled
# Large resource consumption
julia> @time raise_to(10)
0.007901 seconds (7.70 k allocations: 475.745 KiB)
3.5401331746414338e-9
# Subsequent invocations: stable and low timings
julia> @time raise_to(10)
0.000003 seconds (5 allocations: 176 bytes)
3.5401331746414338e-9
julia> @time raise_to(10)
0.000002 seconds (5 allocations: 176 bytes)
3.5401331746414338e-9
julia> @time raise_to(10)
0.000001 seconds (5 allocations: 176 bytes)
3.5401331746414338e-9
```

- Julia: is there a function to obtain the version number of a package?
- Get permutation of one array based on the size ordering of the other array
- Making Julia show different plots in different windows
- How to get equally scaled axes with Plots in Julia
- Scatter plot of two rows of a DataFrame in Julia using Plotly
- Adding constraints to jump model from dict
- Non-iterable argument to a function called by Julia `map`?
- Compute row sums and column sums efficiently in Julia
- Julia copying folder into an existing folder
- How to replace a function in Julia?
- How to force Julia to use multiple threads for matrix multiplication?
- Can Revise.jl handle `ERROR: LoadError: invalid redefinition of constant`?
- Define piecewise function with automatic broadcasting in Julia
- julia Handling time difference in dataframe
- Fast tensor-dot on sparse arrays with GPU in any programming language?
- Why do allocations occur during broadcasting assignment to a preallocated array?
- Comparing RK4 to Euler method
- How to put even numbers from matrix in separate vector in Julia?
- Number of iterations performed by a for-loop in Julia
- Can I redefine a function, but still use the old definition within the new definition?
- how do I use analytical form as gradient with ! function?
- Julia equivalent to R `as.numeric()`
- Why does Float16(1.1)-Float16(1)=Float16(0.0996)?
- Julia manual and defining an infix operator
- In Julia, how to convert a unsigned number to a signed number like in C?
- Julia Package DataFrames 1.6.1 doesn't recognize old version DataFrames 1.3.4 file
- Convert Julia DataFrame to an array of bytes for compression
- How to suppress svg output by IJulia in Jupyter notebooks
- how to get the documentation of a julia function in a vscode notebook?
- Passing command-line arguments to a Pluto Notebook