为什么julia无法识别作为函数参数传递的数组类型,而是将其列出为Any? [英] Why does julia not recognize the type of an array that is passed as a function argument, listing it as Any instead?

查看:121
本文介绍了为什么julia无法识别作为函数参数传递的数组类型,而是将其列出为Any?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在julia中定义了一个接受矢量(特别是Vector{Complex128})的函数.当我查看@code_warntype的输出时,我看到变量类型被列为Any.据我了解,这可能会对速度产生影响.这是代码的简单版本,例如:

I am defining a function in julia that accepts a vector (specifically Vector{Complex128}). When I look at the output of @code_warntype I see that the variable type is listed as Any. This can potentially have speed implications, as I understand. Here is a simple version of the code, for example:

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.

The type of the variable h is listed as Any. I am new to julia and don't really know if I am missing something here. This behaviour doesn't seem specific to Vector{Complex128}, I get the same behaviour with Vector{Float64} as well. Am I annotating the variable type incorrectly here, or is this how it is supposed to work? I'm using julia v0.6.0, if that matters.

推荐答案

这是因为编译器已经对其进行了优化,因此它甚至不存在. v0.6.1中更改了打印方式,使其更加清晰:

This is because the compiler has optimized it away so it doesn't even exist. The way it's printed has changed in v0.6.1 to be more clear:

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

这篇关于为什么julia无法识别作为函数参数传递的数组类型,而是将其列出为Any?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆