In a separate post (https://discourse.julialang.org/t/type-stability-issues-when-using-staticarrays/19126), I learned some things about value type argument. I still have a doubt. In the following code, it seems to me that foo2 is the correct usage of Val{ }. However, it seems that the benchmarks are ok for foo1 as well. Is this correct?
using StaticArrays
using BenchmarkTools
using Test
function foo1(::Val{n}) where n
xi = gausspoints1d(Val(n))
w = gaussweights1d(Val(n))
end
function foo2(::Val{n}) where n
xi = gausspoints1d_2(Val(n))
w = gaussweights1d_2(Val(n))
end
function gausspoints1d_2(::Val{n}) where n
return gausspoints1d(Val(n))
end
function gaussweights1d_2(::Val{n}) where n
return gaussweights1d(Val(n))
end
function gausspoints1d(::Val{9})
return @SVector [-0.968160239507626089835576202904,
-0.836031107326635794299429788070,
-0.613371432700590397308702039341,
-0.324253423403808929038538014643,
0.0,
0.324253423403808929038538014643,
0.613371432700590397308702039341,
0.836031107326635794299429788070,
0.968160239507626089835576202904]
end
function gausspoints1d(::Val{10})
return @SVector [-0.973906528517171720077964012084,
-0.865063366688984510732096688423,
-0.679409568299024406234327365115,
-0.433395394129247290799265943166,
-0.148874338981631210884826001130,
0.148874338981631210884826001130,
0.433395394129247290799265943166,
0.679409568299024406234327365115,
0.865063366688984510732096688423,
0.973906528517171720077964012084]
end
function gaussweights1d(::Val{9})
return @SVector [0.0812743883615744119718921581105,
0.180648160694857404058472031243,
0.260610696402935462318742869419,
0.312347077040002840068630406584,
0.330239355001259763164525069287,
0.312347077040002840068630406584,
0.260610696402935462318742869419,
0.180648160694857404058472031243,
0.0812743883615744119718921581105]
end
function gaussweights1d(::Val{10})
return @SVector [0.0666713443086881375935688098933,
0.149451349150580593145776339658,
0.219086362515982043995534934228,
0.269266719309996355091226921569,
0.295524224714752870173892994651,
0.295524224714752870173892994651,
0.269266719309996355091226921569,
0.219086362515982043995534934228,
0.149451349150580593145776339658,
0.0666713443086881375935688098933]
end
# tests
n = 10
time1() = @btime foo1(Val($n))
time2() = @btime foo2(Val($n))
time1()
time2()
@inferred foo1(Val(n))
@inferred foo2(Val(n))