# Using TimerOutputs, registers allocs, where @allocated does not?

**URL:** <https://discourse.julialang.org/t/using-timeroutputs-registers-allocs-where-allocated-does-not/111995>\
**Category:** General Usage\
**Created:** [March 22, 2024, 11:00pm UTC](https://discourse.julialang.org/t/using-timeroutputs-registers-allocs-where-allocated-does-not/111995 "2024-03-22T23:00:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [March 22, 2024, 11:00pm UTC](https://discourse.julialang.org/t/using-timeroutputs-registers-allocs-where-allocated-does-not/111995/1 "2024-03-22T23:00:08Z")

</div>

Hello!

I have a bit of a complex code, where I use TimerOutputs to track progress. I have the following code bit:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/c/fc1ef19ed40b0947a43d708feebb27f3455140a4.png)

In Line 343 I am updating the position variable. Position, Velocity, Acceleration are SVectors inside of Vectors and the other variables are floating point values. TimerOutputs gives the following:

![image](https://global.discourse-cdn.com/julialang/original/3X/2/4/24edac2eafb5d8341ffdbfc2ebbb831a95aa701e.png)

If I comment line 343 out:

![image](https://global.discourse-cdn.com/julialang/original/3X/6/0/6024654011eb1f36de668881b70c87f85c1e8c08.png)

Next I try to check if allocations occur using `@allocated` as such:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/8/a89821983def20f1c19d85e0a7e5c1c90f076218.png)  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/d/8d4bd5039221362ca19cf73d1947689f199dbfc0.png)

No print statements appear in terminal, i.e. `@allocated` returns zero, but the SimulationLoop still returns average of 985 bytes.

I am a bit perplexed how this can happen, and I wonder if anyone has experienced something similar?

I have done a small check and noted that:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/c/9c6fd1d13576cf3d9a2dd26f17a3501b18ccfe39.png)  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/e/5e389b1d2839fec3f67508d21611d2167969aadf.png)

Where it is seen that adding `SVector(0,0)` to Position does not allocate, but adding 1,1 does.

I don’t really have a MWE sorry, since this is quite nested code in a branch of a repo I am working on, but sharing here since some can probably provide me some guidance on whether what I am seeing is actual or if it is a bug in TimerOutputs.

Kind regards

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [March 23, 2024, 12:32am UTC](https://discourse.julialang.org/t/using-timeroutputs-registers-allocs-where-allocated-does-not/111995/2 "2024-03-23T00:32:56Z")

</div>

After debugging for a while I found it. By using `TimerOutputs` at each function call instead of the total function:

![image](https://global.discourse-cdn.com/julialang/original/3X/6/2/62e8efd5258c12872fbf694c436c613b4aa9a289.png)

I found 989 bytes. It is a weird number and it comes from:

```julia
function UpdateNeighbors!(args...)
    ExtractCells!(Cells,Position,CutOff)

    sortperm!(SortedIndices,Cells) #The first call to sortperm! compiles a new method when working with CartesianIndex{Dimensions} apparently
    # first iteration @time: -> 0.000139 seconds (2 allocations: 23.734 KiB)

```

So yeah, tracking allocations is rewarding and frustrating haha

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [March 24, 2024, 11:17am UTC](https://discourse.julialang.org/t/using-timeroutputs-registers-allocs-where-allocated-does-not/111995/3 "2024-03-24T11:17:44Z")

</div>

For future reference, solution here:

> [@Why does sortperm! allocate here?](https://discourse.julialang.org/t/why-does-sortperm-allocate-here/112028):
>
> Hello! I think I am missing something really simple, I just don’t get why allocations occur, when I have preallocated index array. function main() Cartesians = Vector{CartesianIndex{2}}(undef, 100) SortedIndices = collect(LinearIndices(Cartesians)) # Fill the array with random CartesianIndex{2} values for i in 1:100 # Assuming you want indices in the range 1:10 for both dimensions Cartesians[i] = CartesianIndex(rand(1:10), rand(1:10)) end for iter =…
