# Benchmarking questions

**URL:** https://discourse.julialang.org/t/benchmarking-questions/103961
**Category:** General Usage
**Tags:** benchmark
**Created:** [September 18, 2023, 1:15am UTC](https://discourse.julialang.org/t/benchmarking-questions/103961 "2023-09-18T01:15:24Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Leticia-maria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leticia-maria/32/30981_2.png) [@Leticia-maria](https://discourse.julialang.org/u/Leticia-maria)
#### Post date: [September 18, 2023, 1:15am UTC](https://discourse.julialang.org/t/benchmarking-questions/103961/1 "2023-09-18T01:15:24Z")

</div>

Dear all, I was testing some acceleration of for loops (and reducing the allocations), and I made a less complex example for testing:

```julia
Julia>julia> function test_1(a,b)
           @views for i in 1:a, j in 1:b
               c = i + j
           end
       end
test_1 (generic function with 1 method)
julia> @benchmark test_1(5,5)
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
 Range (min … max): 1.500 ns … 8.291 ns ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 1.583 ns ┊ GC (median): 0.00%
 Time (mean ± σ): 1.566 ns ± 0.084 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

                     ▇ █▁
  ▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁██▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁██▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂ ▂
  1.5 ns Histogram: frequency by time 1.62 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> function test_2(a,b)
           for i in 1:a, j in 1:b
               c = i + j
           end
       end
test_2 (generic function with 1 method)

julia> @benchmark test_2(5,5)
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
 Range (min … max): 1.500 ns … 6.792 ns ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 1.583 ns ┊ GC (median): 0.00%
 Time (mean ± σ): 1.566 ns ± 0.076 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

                     ▇ █▁
  ▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁██▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁██▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂ ▂
  1.5 ns Histogram: frequency by time 1.62 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

```

Shouldn’t I expect the function `test_1` to be faster due to the usage of `@views`?

---

<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: [September 18, 2023, 1:17am UTC](https://discourse.julialang.org/t/benchmarking-questions/103961/2 "2023-09-18T01:17:12Z")

</div>

`@views` only makes things faster when you are actually allocating any slices. There aren’t any here.

---

<div class="post-metadata">

### Author: ![alfaromartino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alfaromartino/32/52986_2.png) [@alfaromartino](https://discourse.julialang.org/u/alfaromartino)
#### Post date: [September 18, 2023, 3:16am UTC](https://discourse.julialang.org/t/benchmarking-questions/103961/3 "2023-09-18T03:16:06Z")

</div>

Just to expand on what’s above, note that scalars don’t allocate.

So if you access a variable holding a number or a single element of a vector (for example, `x=[1,2,3]` and you access `x[1]`), then you don’t need views.

You’d need a view if you have a slice with two or more elements, such as `x[1:2]` .

---

<div class="post-metadata">

### Author: ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)
#### Post date: [September 18, 2023, 6:20am UTC](https://discourse.julialang.org/t/benchmarking-questions/103961/4 "2023-09-18T06:20:08Z")

</div>

Note also that your benchmarks are incorrect since Julia optimizes your loops away because your functions don’t return anything, and so independently by the magnitude of `a` and `b` it takes a small amount of `ns`:

```julia

julia> @benchmark test_1(1000000000,10000000000)
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
 Range (min … max): 4.700 ns … 73.700 ns ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 5.700 ns ┊ GC (median): 0.00%
 Time (mean ± σ): 5.514 ns ± 1.185 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

  ▅ █ ▁ ▂ ▂ ▃ ▃ ▄ ▆ █ ▇ ▅ ▄ ▃ ▁ ▂
  █▁█▁▁▅▁█▁▁█▁█▁▁█▁█▁▁█▁█▁▁█▁█▁▁█▁█▁▁█▁█▁▁█▁█▁▁▇▁▇▁▁▆▁▇▁▁█▁█ █
  4.7 ns Histogram: log(frequency) by time 7 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

```
