# Question about "in place" operations

**URL:** <https://discourse.julialang.org/t/question-about-in-place-operations/103573>\
**Category:** Performance\
**Tags:** performance, optimization\
**Created:** [September 6, 2023, 3:14pm UTC](https://discourse.julialang.org/t/question-about-in-place-operations/103573 "2023-09-06T15:14:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fdekerme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fdekerme/32/43574_2.png) [@fdekerme](https://discourse.julialang.org/u/fdekerme)\
**Post date:** [September 6, 2023, 3:14pm UTC](https://discourse.julialang.org/t/question-about-in-place-operations/103573/1 "2023-09-06T15:14:20Z")

</div>

Hello,  
At the moment I’m studying a little bit of code optimisation. I don’t understand why in case with `sample()` we have 16 bytes allocated and in the second case 0 (with the in place operation “`sample!()`”). Isn’t it possible to change the value of a float or an int without reallocating memory? Do you have to use a single-value vector to do this?

Thank you!  
fdekerm

```julia
julia> using StatsBase

julia> x = [0.0]
1-element Vector{Float64}:
 0.0

julia> y = rand(10)
10-element Vector{Float64}:
 0.711827244320969
 0.748420926219918
 0.3664891177657702
 0.787337039347841
 0.25127202582425534
 0.02232231788819372
 0.5835537053001686
 0.5261181974982757
 0.6587823584053296
 0.16470270236878148

julia> z = 0.0
0.0

julia> @allocated z = sample(x)
16

julia> @allocated sample!(x,y)
0

```

---

<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:** [September 6, 2023, 3:50pm UTC](https://discourse.julialang.org/t/question-about-in-place-operations/103573/2 "2023-09-06T15:50:16Z")

</div>

That’s just an artifact of the benchmark:

```julia
julia> using BenchmarkTools

julia> y = rand(10);

julia> @ballocated z = sample($y)
0

```
