I believe the variable “r” is local scope inside the function because I passed it into the function as an argument to the function. As far as “data”, as far as I’ve seen and heard, Julia structures are automatically constant globals! Constant globals run just as fast as locals – as far as I’m aware.
I’ve done the benchmarking, and this assumption appears to be true, because the benchmark times for the following code shows great performance with my global structure “data” vs local variables.
Compare this code and benchmark:
module test
module stuff1
struct teststruct
x::Array{Float64,1} #this will be a 1000-element array that behaves like a const global array. I can mutate it's elements!
y::Array{Float64,1} #this will be a 1-element array that behaves like a const global array. I can mutate it's elements!
z::Float64 #this will be a value that I cannot mutate!
end
data=teststruct(rand(1000),[6.7],9.1);
println(data.x[1:10])
end
module run_calculations1
import ..stuff1.data
#define further parameters here
r=8.0
function run_simulation(data,r)
s = 0.0
y=1.0
for i in 1:length(data.x)
for j in 1:length(data.x)
data.x[i] = rand(1)[1]*y*data.z+data.y[1]+r
end
end
return data
end
end
time() = @time y=run_calculations1.run_simulation(stuff1.data,run_calculations1.r)
time()
time()
time()
time()
end
OUTPUTS:
[0.323492, 0.235494, 0.51633, 0.784921, 0.66276, 0.312472, 0.72803, 0.40744, 0.974881, 0.852874]
0.139204 seconds (1.04 M allocations: 93.487 MiB, 22.53% gc time)
0.101172 seconds (1000.00 k allocations: 91.553 MiB, 31.57% gc time)
0.097960 seconds (1000.00 k allocations: 91.553 MiB, 31.24% gc time)
0.085701 seconds (1000.00 k allocations: 91.553 MiB, 27.62% gc time)
with this one:
module test
module stuff1
struct teststruct
x::Array{Float64,1} #this will be a 1000-element array that behaves like a const global array. I can mutate it's elements!
y::Array{Float64,1} #this will be a 1-element array that behaves like a const global array. I can mutate it's elements!
z::Float64 #this will be a value that I cannot mutate!
end
data=teststruct(rand(1000),[6.7],9.1);
println(data.x[1:10])
end
module run_calculations1
import ..stuff1.data
#define further parameters here
function run_simulation()
s = 0.0
y=1.0
x=[1.0]
for i in 1:1000
for j in 1:1000
x[1] = rand(1)[1]*y*9.1+6.7+8.0
end
end
return x
end
end
time() = @time y=run_calculations1.run_simulation()
time()
time()
time()
time()
end
OUTPUTS:
[0.598739, 0.238504, 0.570769, 0.768084, 0.30716, 0.0337915, 0.361736, 0.715133, 0.96916, 0.987557]
0.100308 seconds (1.00 M allocations: 91.553 MiB, 26.71% gc time)
0.091171 seconds (1.00 M allocations: 91.553 MiB, 21.15% gc time)
0.077898 seconds (1.00 M allocations: 91.553 MiB, 22.52% gc time)
0.084541 seconds (1.00 M allocations: 91.553 MiB, 22.64% gc time)
@kristoffer.carlsson I’d be excited to learn some way to improve this example code’s performance. It’s the best I got now.