我正在 julia 中定义一个接受向量的函数(具体为 Vector{Complex128}
)。当我查看 @code_warntype
的输出时,我发现变量类型被列为 Any
。据我了解,这可能会对速度产生影响。这是代码的简单版本,例如:
function abc(h::Vector{Complex128})
a=1+2
end
@code_warntype
的输出是
julia> @code_warntype abc(zeros(Complex128,2))
Variables:
#self#::#abc
h::Any
a::Int64
Body:
begin
SSAValue(0) = (Base.add_int)(1, 2)::Int64
return SSAValue(0)
end::Int64
变量h
的类型被列为Any
。我是 Julia 的新手,真的不知道我是否在这里遗漏了一些东西。此行为似乎并不是 Vector{Complex128}
特有的,我在 Vector{Float64}
中也得到了相同的行为。我在这里注释变量类型是否不正确,或者这就是它应该如何工作?我正在使用 julia v0.6.0,如果这很重要的话。
最佳答案
这是因为编译器已将其优化掉,因此它甚至不存在。 v0.6.1 中的打印方式已更改,更加清晰:
julia> function abc(h::Vector{Complex128})
a=1+2
end
abc (generic function with 1 method)
julia> @code_warntype abc(zeros(Complex128,2))
Variables:
#self# <optimized out>
h <optimized out>
a <optimized out>
Body:
begin
SSAValue(0) = (Base.add_int)(1, 2)::Int64
return SSAValue(0)
end::Int64
关于arrays - 为什么 julia 无法识别作为函数参数传递的数组类型,而是将其列为 Any?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47201832/