Hello, everyone
Very simple code, the speed is really slow, I suspect that the method of using Ref in the structure is wrong
Everyone, please take a look, thank you
mutable struct T3
f1::Int64
f2::String
f3::Float64
EvlP1::Ref{Float64}
f5::Vector{Int64}
function T3()
this = new()
this.EvlP1 = Ref{Float64}(3.0)
return this
end
end
function test()
t3=T3()
#cc=0.1::Float64
cc=Float64(0.1)
for i = 1:10^8
cc = cc +t3.EvlP1[]
end
println(cc)
end
#@time test()
@code_warntype test()
The following is the result of code_warntype, 3 places are red
Variables
#self#::Core.Compiler.Const(test, false)
t3::T3
cc::Any
@_4::Union{Nothing, Tuple{Int64,Int64}}
i::Int64
Body::Nothing
1 ββ (t3 = Main.T3())
ββ (cc = Main.Float64(0.1))
ββ %3 = Core.apply_type(Base.Val, 8)::Core.Compiler.Const(Val{8}, false)
ββ %4 = (%3)()::Core.Compiler.Const(Val{8}(), false)
ββ %5 = Base.literal_pow(Main.:^, 10, %4)::Int64
ββ %6 = (1:%5)::Core.Compiler.PartialStruct(UnitRange{Int64}, Any[Core.Compiler.Const(1, false), Int64])
ββ (@_4 = Base.iterate(%6))
ββ %8 = (@_4 === nothing)::Bool
ββ %9 = Base.not_int(%8)::Bool
ββββββ goto #4 if not %9
2 ββ %11 = @_4::Tuple{Int64,Int64}::Tuple{Int64,Int64}
ββ (i = Core.getfield(%11, 1))
ββ %13 = Core.getfield(%11, 2)::Int64
ββ %14 = cc::Any <--------------
ββ %15 = Base.getproperty(t3, :EvlP1)::Ref{Float64} <--------------
ββ %16 = Base.getindex(%15)::Any <--------------
ββ (cc = %14 + %16)
ββ (@_4 = Base.iterate(%6, %13))
ββ %19 = (@_4 === nothing)::Bool
ββ %20 = Base.not_int(%19)::Bool
ββββββ goto #4 if not %20
3 ββ goto #2
4 ββ %23 = Main.println(cc)::Core.Compiler.Const(nothing, false)
ββββββ return %23