# @time returns a negative value

**URL:** https://discourse.julialang.org/t/time-returns-a-negative-value/84935
**Category:** New to Julia
**Tags:** question, benchmark
**Created:** [July 28, 2022, 8:03pm UTC](https://discourse.julialang.org/t/time-returns-a-negative-value/84935 "2022-07-28T20:03:16Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![trashspam7](https://avatars.discourse-cdn.com/v4/letter/t/e79b87/32.png) [@trashspam7](https://discourse.julialang.org/u/trashspam7)
#### Post date: [July 28, 2022, 8:03pm UTC](https://discourse.julialang.org/t/time-returns-a-negative-value/84935/1 "2022-07-28T20:03:16Z")

</div>

I’m using `@time` to time some code, what does it mean when it returns a negative value? What does it mean when it returns `nothing`? What is the trusted resolution of `@time`? I just need to get precision on the level of ~1s.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [July 28, 2022, 8:09pm UTC](https://discourse.julialang.org/t/time-returns-a-negative-value/84935/2 "2022-07-28T20:09:02Z")

</div>

`@time` does nothing more than get the current time, run your code, get the current time after that and subtracting the former from the latter (plus some GC/compiler statistics). You can see exactly what it does with `@macroexpand`.

If it prints a negative time, it would mean that either your computer has its clock running backwards, or a datetime correction in the OS was performed while your code ran.

The timer itself guarantees (in current versions) millisecond resolution, as far as Im aware.

---

<div class="post-metadata">

### Author: ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)
#### Post date: [July 28, 2022, 8:14pm UTC](https://discourse.julialang.org/t/time-returns-a-negative-value/84935/3 "2022-07-28T20:14:07Z")

</div>

It’s like you have traveled in time 🙂 The simplest expression

```julia
julia> @time 0
  0.000001 seconds

```

even returns a positive result in my case. I have tried it for empty string and as well as `nothing`. In the documentation of `@time` it is stated that

```julia
  │ Note
  │
  │ For more serious benchmarking, consider the @btime macro from the BenchmarkTools.jl package which among other
  │ things evaluates the function multiple times in order to reduce noise.

```

and it is better to use `@btime` macro for benchmarking issues.

---

<div class="post-metadata">

### Author: ![trashspam7](https://avatars.discourse-cdn.com/v4/letter/t/e79b87/32.png) [@trashspam7](https://discourse.julialang.org/u/trashspam7)
#### Post date: [July 28, 2022, 8:22pm UTC](https://discourse.julialang.org/t/time-returns-a-negative-value/84935/4 "2022-07-28T20:22:53Z")

</div>

Ok I found my mistake, and I feel really silly. My code was something like

```julia
time = @time blah()

```

and I assumed the `time` variable would hold the time evaluated, but it holds the return value of the function `blah`! Is there a way to get the result of @time into a variable?

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [July 28, 2022, 8:26pm UTC](https://discourse.julialang.org/t/time-returns-a-negative-value/84935/5 "2022-07-28T20:26:54Z")

</div>

`@time` should never be able to report a time of `nothing`. The actual return value of `@time` is just the value of the expression and has nothing to do with the time it took to execute. That time is printed but never returned.

```julia
julia> a = @time 3-4
  0.000001 seconds
-1

julia> a
-1

```

I suspect you’re trying to read the output of `@time` into a variable. Perhaps you are looking for `@elapsed`?

```julia
julia> b = @elapsed 3-4
5.0e-7 # this is how long the expression took to execute, in seconds

```
