@time vs @btime

Hi all,
I know that @btime is better than builtin @time because latest compute some overhead.

But in my scenario most of my code is computed only once, and I have to take into account this overhead. Also forcing @btime to make only one sample, it make some tuning and require an interpolation that cause a faster execution of measured code snippet, but these things are not present in real code.

A MWE follows:

using BenchmarkTools

function f()
	a = [1:100;]
	
	@time f = (a .< 70)
	@btime f = ($a .< 70)
	
	return nothing
end

BenchmarkTools.DEFAULT_PARAMETERS.samples = 1
f()

Then, it’s correct that in my scenario is better to use @time (more similar to simple tic() / toc(), confirmed reading implementation code of @time) than @btime? or there are some other differences that return uncorrect measured times?

Many thanks in advance

Leonardo

3 Likes

If you know you will run a function once or a few times, and care about compilation overhead, then it may be justifiable to use @time. In practice, however, runtime is much easier to optimize than compilation time — the former is addressed in the manual, while the latter requires a lot of investment into learning about Julia’s internals. So most users choose to focus on what they can change easily, hence @btime.

4 Likes

It sort of depends on what you mean by “compute only once”. Are you writing a piece of code that will only run one time ever? In that case, I don’t see what purpose is served by benchmarking it.

Or do you mean that it will only run once per Julia session?

1 Like

I start Julia executable (called by a script) that execute code and exit.

Benchmark is useful to better study some performance oddities resulting from executions in Julia 0.5 (last stable version when code was created) and Julia 0.6 (actual last stable version) - see another my post

Leonardo

Then use the time command of your Bash shell. For example, this times how long it takes to julia to start up and execute sin.(rand(100000)) and exit:

 >> time julia -e "sin.(rand(100000))"                                                                                                
julia -e "sin.(rand(100000))"  3.06s user 0.08s system 99% cpu 3.154 total

Aside: note that usually, one times at the REPL and not inside functions. I’m not sure @btime is made to be used inside functions. For example as Greg does in your other thread: Array performance Julia 0.6 vs 0.5 - #5 by greg_plowman.

If you care about performance and the startup time matters, don’t do this. Write your script in Julia.

2 Likes