# @time vs @btime

**URL:** <https://discourse.julialang.org/t/time-vs-btime/9879>\
**Category:** Performance\
**Created:** [March 22, 2018, 7:37am UTC](https://discourse.julialang.org/t/time-vs-btime/9879 "2018-03-22T07:37:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![leoc](https://avatars.discourse-cdn.com/v4/letter/l/da6949/32.png) [@leoc](https://discourse.julialang.org/u/leoc)\
**Post date:** [March 22, 2018, 7:37am UTC](https://discourse.julialang.org/t/time-vs-btime/9879/1 "2018-03-22T07:37:22Z")

</div>

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:

```julia
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

---

<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:** [March 22, 2018, 7:47am UTC](https://discourse.julialang.org/t/time-vs-btime/9879/2 "2018-03-22T07:47:20Z")

</div>

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](https://docs.julialang.org/en/latest/manual/performance-tips/#man-performance-tips-1), 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`.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 22, 2018, 8:02am UTC](https://discourse.julialang.org/t/time-vs-btime/9879/3 "2018-03-22T08:02:26Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![leoc](https://avatars.discourse-cdn.com/v4/letter/l/da6949/32.png) [@leoc](https://discourse.julialang.org/u/leoc)\
**Post date:** [March 22, 2018, 8:17am UTC](https://discourse.julialang.org/t/time-vs-btime/9879/4 "2018-03-22T08:17:12Z")

</div>

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](https://discourse.julialang.org/t/array-performance-julia-0-6-vs-0-5/9796)

Leonardo

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [March 22, 2018, 9:48am UTC](https://discourse.julialang.org/t/time-vs-btime/9879/5 "2018-03-22T09:48:38Z")

</div>

> [@leoc](#):
>
> I start Julia executable (called by a script) that execute code and exit.

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:

```julia
 >> 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](https://discourse.julialang.org/t/array-performance-julia-0-6-vs-0-5/9796/5).

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 22, 2018, 12:10pm UTC](https://discourse.julialang.org/t/time-vs-btime/9879/6 "2018-03-22T12:10:31Z")

</div>

> [@leoc](#):
>
> I start Julia executable (called by a script) that execute code and exit

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