在 Julia 中使用 @code_warntype [英] Usage of @code_warntype in Julia

查看:11
本文介绍了在 Julia 中使用 @code_warntype的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试编写类型稳定的代码时,使用 @code_warntype 进行检查,我应该只担心没有任何 AnyUnion变量部分还是我也应该检查正文部分?

When trying to write type-stable code, examining using @code_warntype, should I only worry about not having any Any or Union in the variables section or I should also check the body section?

推荐答案

根据您的 julia 版本,@code_warntype 有时可以标记某些无害的表达式.通过一些练习,您可以轻松识别这些表达式(例如,它们通常与模块/名称查找有关).

Depending on your version of julia, @code_warntype can sometimes flag certain expressions that are harmless. With a little bit of practice you can easily recognize these expressions (e.g., they often have to do with module/name lookup).

正如 Reza 所说,最简单的方法是查看变量部分和函数的返回类型;如果您没有看到任何类型不确定的数量(AnyUnions),您可能无需担心.如果您想深入挖掘,请开始尝试理解它所标记的身体表情.对于学习,一件有用的事情是将结果与使用 track-allocation 获得的结果进行比较,因为真正的类型不稳定性与内存分配有关.

The easy path, as Reza says, is to look at the variables section and the function's return-type; if you don't see any type-uncertain quantities (either Any or Unions) you probably have nothing to worry about. If you want to dig deeper, start trying to make sense of the body expressions that it's flagging. For learning, one useful thing to do is compare the results against those obtained with track-allocation, since true type instability is associated with memory allocation.

不过,我认为 @code_warntype 的问题已经在当前 master 上得到修复,所以无论如何,julia 的下一个版本应该更容易解释.

However, I think the issues with @code_warntype have been fixed on current master, so in any case the next release of julia should make it easier to interpret.

这篇关于在 Julia 中使用 @code_warntype的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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