string - Julia - 为什么使用宏 @info 会产生糟糕的 @code_warntype 性能?

标签 string performance julia display

我想知道为什么使用 @info 会产生糟糕的 err 变量性能:Any。请参阅下一个 MWE:

function bad_performance()
    @info "demo"
end



julia> @code_warntype bad_performance()
Variables
  #self#::Core.Compiler.Const(test, false)
  level::Base.CoreLogging.LogLevel
  std_level::Base.CoreLogging.LogLevel
  group::Symbol
  _module::Module
  logger::Union{Nothing, Base.CoreLogging.AbstractLogger}
  id::Symbol
  file::String
  line::Int64
  msg::String
  err::Any

我应该改变什么来避免它,或者我应该忽略来自@code_warntype的红色警告?

注意:它还会为记录器变量生成黄色警告。

最佳答案

这是因为您可以创建自己的记录器并将它们设置为当前记录器,该记录器使用全局非常量变量。您可以更简单地看到相同的现象

julia> printstdout(str) = print(stdout, str)
printstdout (generic function with 1 method)

julia> @code_warntype optimize=true printstdout("hello")
Variables
  #self#::Core.Const(printstdout)
  str::String

Body::Union{Nothing, Int64}
1 ─ %1  = Main.print::Core.Const(print)
│   %2  = Main.stdout::Any
│   %3  = (isa)(%2, Cthulhu.TextWidthLimiter)::Bool
└──       goto #3 if not %3
2 ─ %5  = π (%2, Cthulhu.TextWidthLimiter)
│   %6  = invoke %1(%5::Cthulhu.TextWidthLimiter, _2::String)::Union{Nothing, Int64}
└──       goto #4
3 ─ %8  = Main.print(Main.stdout, str)::Union{Nothing, Int64}
└──       goto #4
4 ┄ %10 = φ (#2 => %6, #3 => %8)::Union{Nothing, Int64}
└──       return %10

尽管如此

julia> typeof(stdout)
Base.TTY

它被推断为 Any 因为

julia> isconst(Base, :stdout)
false

必须采用这种方式才能允许redirect_stdout

好处是,糟糕的记录器推断仅在您实际记录时才重要;如果它包含在不被触发的 if block 中,则对性能的影响通常很小。如果您发现这对您的情况很重要,请将 @info 放入一个单独的函数中,您可以从需要快速的函数中调用该函数。

关于string - Julia - 为什么使用宏 @info 会产生糟糕的 @code_warntype 性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66140984/

相关文章:

java - 字符串中重复次数最多的字符

string - 环绕字符串的唯一子字符串

c - 在 MVS 2010 Professional v/s Express for C 中发布版本

java - 我在将 SearchView 与 RecyclerView 一起应用时遇到问题

javascript - 找不到 D3.js 内存泄漏

javascript - 在 Javascript 中获取两个字符串之间的字符串的正则表达式

php - laravel 检索请求输入返回 null

julia - SimpleHypergraphs.jl - 从文本文件加载超图

MySQL 加载数据本地 INFILE Julia

julia - Julia 1.0 中 "workspace()"的替代方案是什么?