调试登录Scala,没有性能影响

编程入门 行业动态 更新时间:2024-10-28 01:18:21
本文介绍了调试登录Scala,没有性能影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在诸如C / C ++ / Objective-C之类的语言中,通常使用预处理器宏定义甚至不为已发布的二进制文件编译的日志记录机制,从而不会导致性能下降。以下行:

#ifdef DEBUG printf(some event we want to log\\\ ); #endif

现在,我知道Scala中没有预处理器。所以我的问题是:实现一个机制来记录程序活动以进行调试的最好方法是什么,当关闭时至少影响性能?

解决方案

您可以使用 scala.annotation.elidable

可以删除调用方法的注释生成的代码。

行为受到将-Xelide-below传递给scalac的影响。如果给予注释的优先级低于命令行参数,则标记为elidable的方法将从生成的代码中省略。示例:

In languages like C/C++/Objective-C it's common to use preprocessor macros to define logging mechanisms that are not even compiled for released binaries, thus causing no performance hit. Something along the lines of:

#ifdef DEBUG printf("some event we want to log\n"); #endif

Now, I know there's no preprocessor in Scala. So my question is: what is the best way to implement a mechanism to log program activity for debug purposes, while impacting performance the least when it's turned off?

解决方案

You can use scala.annotation.elidable

An annotation for methods for which invocations might be removed in the generated code.

Behavior is influenced by passing -Xelide-below to scalac. Methods marked elidable will be omitted from generated code if the priority given the annotation is lower than to the command line argument. Examples:

更多推荐

调试登录Scala,没有性能影响

本文发布于:2023-11-25 14:26:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630119.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:性能   Scala

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!