mutable struct Type4
c
a
b
function Type4(x=1,y=1,z=1)
new(x,y,z)
end
end
mutable struct Type5
a
b
c
function Type5(x::Int64=1, y::Int64=1, z::Int64=1)
new(x,y,z)
end
end
mutable struct Type8
a::Real
b::Real
c::Real
function Type8(x=1,y=1,z=1)
new(x,y,z)
end
end
mutable struct Type9
a::Real
b::Real
c::Real
function Type9(x::Int64=1,y::Int64=1,z::Int64=1)
new(x,y,z)
end
end
using BenchmarkTools
@benchmark Type4()
@benchmark Type5()
@benchmark Type8()
@benchmark Type9()
Not a huge difference but consistent difference.
@kwdef mutable struct Type10
a::Real=1
b::Real=1
c::Real=1
end
mutable struct Type11
a
b
c
function Type11(x::Real=1, y::Real=1, z::Real=1)
new(x,y,z)
end
function Type11(x::Number, y::Number,z::Number)
new(x,y,z)
end
end
mutable struct Type12
a::Number
b::Number
c::Number
function Type12(x::Real=1, y::Real=1, z::Real=1)
new(x,y,z)
end
function Type12(x::Number, y::Number,z::Number)
new(x,y,z)
end
end
@benchmark Type10()
@benchmark Type11()
@benchmark Type12()
To me, it seemed like not defining attribute types while defining the struct attributes, but later in the constructor is the best approach.
(Forgive me for my lingo… I am not well acquainted with the actual terminology we use and am still a beginner.)