# Benchmark in place functions with @btime wasn't set up properly?

**URL:** <https://discourse.julialang.org/t/benchmark-in-place-functions-with-btime-wasnt-set-up-properly/42705>\
**Category:** Performance\
**Created:** [July 8, 2020, 3:54am UTC](https://discourse.julialang.org/t/benchmark-in-place-functions-with-btime-wasnt-set-up-properly/42705 "2020-07-08T03:54:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jchen975](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jchen975/32/8994_2.png) [@jchen975](https://discourse.julialang.org/u/jchen975)\
**Post date:** [July 8, 2020, 3:54am UTC](https://discourse.julialang.org/t/benchmark-in-place-functions-with-btime-wasnt-set-up-properly/42705/1 "2020-07-08T03:54:13Z")

</div>

I wrote an in-place function that performs the multidimensional Newton-Raphson algorithm, and I’m trying to compare the performance with the original implementation in Matlab.

Since I wrote it as an in-place function, I used  
`@btime nr!($data, $options) setup(data=load_f("file.m")) evals=1`  
to force it to reload a new `data` variable from a local file each time, so that updating the `data` with solved results doesn’t affect the next run.

This results in an almost 12x speedup compared to the original Matlab code, which I was pretty excited about at first, but the result is a little worrying: `nr!` returns the mismatch at each NR iteration as well as the number of iterations used to reach the final acceptable mismatch. When I simply did

```julia
data = load_f("file.m")
nr!(data, options) # options contains tolerance, max iterations, etc. defined elsewhere

```

it returns the mismatches and iterations (3) as expected. But with `@btime` and the `setup`, `evals` I got just the final mismatch from before and 0 iteration, which seems to me that the `setup` wasn’t done properly and it was running with a `data` with already solved values.

I also tried to do the following and received similar results.

```julia
data = load_f("file.m")
@btime nr!($data_copy, $options) setup(data_copy=deepcopy(data)) evals=1

```

Could anyone help me understand what’s going on here? What am I doing wrong?

Thanks very much!

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [July 8, 2020, 3:57am UTC](https://discourse.julialang.org/t/benchmark-in-place-functions-with-btime-wasnt-set-up-properly/42705/2 "2020-07-08T03:57:50Z")

</div>

My solution to this (not the most elegant) is to just use `@time` several times (and ignore the first). Not the best answer, but I know it will do what I want.

---

<div class="post-metadata">

**Author:** ![jchen975](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jchen975/32/8994_2.png) [@jchen975](https://discourse.julialang.org/u/jchen975)\
**Post date:** [July 8, 2020, 4:05am UTC](https://discourse.julialang.org/t/benchmark-in-place-functions-with-btime-wasnt-set-up-properly/42705/3 "2020-07-08T04:05:52Z")

</div>

Yes it works, and the result of `@time` actually shows only about 2x speedup compared to the Matlab implementation (LOL but more like what I expected). I still want to use `@btime` if possible though, because even if I get away with just `@time` this time doesn’t mean I can do it in future…

Also, I commented the in-place section of `nr!` out, but interestingly, there’s still quite a bit of discrepancy between `@btime` (610 us) and `@time` (920 us), and the result recorded from

```julia
function nr!(data, options)
    ts = time()
    newton raphson body 
    time() - ts
end

```

is closer to `@time` than `@btime` and actually even larger than `@time`’s result. I’m so confused…
