Code runtime constantly increasing with time

Another alternative to using Setfield is to use one-element vectors, for example:

julia> struct A
         x::Vector{Int}
       end

julia> function upA(a,n)
         for i in 1:n
           a.x[1] += 1
         end
         return a
       end
upA (generic function with 1 method)

julia> @btime upA(a,100_000) setup=(a=A([0])) evals=1
  24.000 ns (0 allocations: 0 bytes)
A([100000])

That could be used for your mutable scalars inside your immutable struct as well. It is a little bit ugly to have to intialize and mutate scalars as if they are arrays, but I in general feel safer with that. I have even recently discussed all the alternatives on how to do that, here.

1 Like