Hi everyone!
I’m trying to define some global states inside my module, but am also concerned about how to elevate the performance…
I have done some research on this, some suggests that to use a struct to pack the states. I try to make the illustration as simple as possible:
mutable struct GlobalState
x::Real
A::Array
end
const myState = GlobalState(1.0, ones(2, 2))
and I define some test functions for bencmark testings:
function test1(myState::GlobalState) # use global state
B = fill(0.0, size((myState.A)))
@views for _ in 1:1000
B .+= myState.x .* myState.A
end
end
function test2(myState::GlobalState) # unpacking??
B = fill(0.0, size((myState.A)))
x = myState.x
A = copy(myState.A)
@views for _ in 1:1000
B .+= x .* A
end
end
function ref() # reference
B = zeros(2, 2)
x = 1.0
A = ones(2, 2)
@views for _ in 1:1000
B .+= x .* A
end
end
The benchmark results are:
601.600 μs (5002 allocations: 203.25 KiB)
600.600 μs (5003 allocations: 203.34 KiB)
6.925 μs (2 allocations: 192 bytes)
In test1
and test2
there will be more memory allocations if the iteration grows. Does this implies a potential longer gc time? (Please correct me if I’m wrong about this ) I thought test2
will be different from test1
since I have done some “unpacking”
The ref
function does the same task with no global states infected. My problem is, how can I have the same performance as the ref
function but still being able to have some sense of “communication” with the outside world? Thanks!!
(UPDATE: I have also tried to remove mutable
from the struct definition, but results are the same)