I'm writing the code for found the solution using chi-square fitting. It includes many parameters and massive equations, thus, the time cost is so high. However, some equations and parameters don't need to be calculated(or read) for every iteration. Just one calculation at the first iteration is enough. In order to reduce the time cost, I want to fix the values, on the other hand, not swap the value. Is there any method and/or trick for this purpose?
CodePudding user response:
I don't think there is any other way other than to have two loops. This may be tricky, but you can first calculate your equations outside the main (second) loop. However since it's only on the first iteration, technically you only have 1 loop as a loop with 1 iteration is basically useless. Afterwards, you can use them in the loop.
#Calculate here
#loop here
...
...
CodePudding user response:
I think you are asking about avoiding re-computation by buffering known results. This technique is called memoization.
This can be achieved by using Memoization
:
using Memoization
@memoize function myfunction(x)
sleep(0.5)
x .^ 2
end
Let us have a look how this works:
julia> @time myfunction(1:3)
0.513210 seconds (6 allocations: 224 bytes)
3-element Vector{Int64}:
1
4
9
julia> @time myfunction(1:3)
0.000004 seconds
3-element Vector{Int64}:
1
4
9
You can see that subsequent calls to myfunction use the memoized buffer.