# Adding constant size vectors

**URL:** <https://discourse.julialang.org/t/adding-constant-size-vectors/80958>\
**Category:** Performance\
**Created:** [May 12, 2022, 2:22pm UTC](https://discourse.julialang.org/t/adding-constant-size-vectors/80958 "2022-05-12T14:22:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fabrice\_Rosay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabrice_rosay/32/15689_2.png) [@Fabrice\_Rosay](https://discourse.julialang.org/u/Fabrice_Rosay)\
**Post date:** [May 12, 2022, 2:22pm UTC](https://discourse.julialang.org/t/adding-constant-size-vectors/80958/1 "2022-05-12T14:22:04Z")

</div>

I need to add two vectors of size 256. What would be the fastest way to do this?  
It needs to be as much optimized as possible as it will be inside an alpha-beta search.  
Sub questions would it be much faster if I use int8 vectors instead of float32? Would it be faster to use tuple instead of vectors.  
Any hint to a solution or code I could take inspiration from would be appreciated.  
Thanks in advance

---

<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:** [May 12, 2022, 2:33pm UTC](https://discourse.julialang.org/t/adding-constant-size-vectors/80958/2 "2022-05-12T14:33:02Z")

</div>

Int8 will be much faster since you can fit more per AVX instruction.

---

<div class="post-metadata">

**Author:** ![Fabrice\_Rosay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabrice_rosay/32/15689_2.png) [@Fabrice\_Rosay](https://discourse.julialang.org/u/Fabrice_Rosay)\
**Post date:** [May 12, 2022, 3:11pm UTC](https://discourse.julialang.org/t/adding-constant-size-vectors/80958/3 "2022-05-12T15:11:17Z")

</div>

Is it something the compiler will do automatically?  
That is can I get something faster than  
`a. +=b`. Usually as long as I don’t allocate, I never find a way to make faster code than pure julia. And yet there are stuff like vectorizationbase…

---

<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:** [May 12, 2022, 3:13pm UTC](https://discourse.julialang.org/t/adding-constant-size-vectors/80958/4 "2022-05-12T15:13:34Z")

</div>

```julia
julia> a=rand(UInt8, 256);
julia> b=rand(UInt8, 256);

julia> @btime a.+=b;
  363.153 ns (2 allocations: 64 bytes)

using LoopVectorization
julia> @btime @turbo a.+=b;
  270.824 ns (2 allocations: 64 bytes)

```

I think the difference is AVX-512, but I’m not 100% sure.

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [May 12, 2022, 3:56pm UTC](https://discourse.julialang.org/t/adding-constant-size-vectors/80958/5 "2022-05-12T15:56:05Z")

</div>

Reminder that global variables should be interpolated while benchmarking:

```julia
julia> ai = rand(UInt8, 256);
julia> bi = rand(UInt8, 256);
julia> af = rand(Float32, 256);
julia> bf = rand(Float32, 256);

julia> using BenchmarkTools, LoopVectorization
julia> @btime $ai .+= $bi;
  7.600 ns (0 allocations: 0 bytes)

julia> @btime @turbo $ai .+= $bi;
  6.800 ns (0 allocations: 0 bytes)

julia> @btime $af .+= $bf;
  15.431 ns (0 allocations: 0 bytes)

julia> @btime @turbo $af .+= $bf;
  14.329 ns (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [May 12, 2022, 4:08pm UTC](https://discourse.julialang.org/t/adding-constant-size-vectors/80958/6 "2022-05-12T16:08:50Z")

</div>

256 sounds slightly too large for [StaticArrays](https://github.com/JuliaArrays/StaticArrays.jl) but maybe it is worth a shot?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [May 12, 2022, 9:40pm UTC](https://discourse.julialang.org/t/adding-constant-size-vectors/80958/7 "2022-05-12T21:40:46Z")

</div>

Just for the note, doesn’t seem that `@turbo` is getting much better than `@simd` (and it increases initial latency, so may not be worthwhile here):

```julia
julia> function sumab(a,b) 
           @inbounds @simd for i in eachindex(a)
               a[i] += b[i] 
           end
           return a
       end
sumab (generic function with 2 methods)

julia> @btime sumab($a,$b);
  5.556 ns (0 allocations: 0 bytes)

julia> @btime @turbo $a .+= $b;
  5.842 ns (0 allocations: 0 bytes)

```
