是否Log.i()影响到Android应用程序的性能? [英] Does Log.i() affect performance on Android applications?

查看:2493
本文介绍了是否Log.i()影响到Android应用程序的性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个老派开发商(好吧,我是20,我不是老同学,我只是喜欢打印出来,而不是使用一步一步的调试器:P)和我有很多的Log.i()调用Android应用程序。我不知道是否会对应用程序的性能产生影响?

我知道我应该用一步一步的调试器,它只是调试多线程可能会有点麻烦了。

感谢您的帮助!


解决方案

我还在寻找到Android的编码自己,但我从我所看到的,到目前为止我认为Android的日志记录,从同样的问题受到影响,大多数Java日志框架苦于(值得注意的例外是SLF4J),分别是:


  • 记录不会影响性能,因为:

    1. 这意味着额外的方法调用。 (不是用于应用程序的99%的问题)。

    2. 这往往意味着字符串的装配工作(大的影响)

    3. 它从事额外的IO(影响很大)


开发一般通过增加防护块处理这种

 如果(log.isDebugEnabled()){
   log.debug(...);
}

我觉得Android SDK中可以做到这一点。它涵盖了#2,#3,但仍然留下含有未记录code你释放code。假设,当然,你永远要启用一个释放情况的调试日志记录。

SFL4J BTW,不需要警卫阻止,因为它用c像延缓串组件,直到它的实际需要方法调用。不幸的是,看起来,Android已经没有走这条道路。 iOS不存在这个问题可能是因为它有一个pre-编译器。东西,我常常希望的Java一直保持。

无论如何,我不会主张有利于调试器去除记录。 IMHO它们服务器两个不同的目的。测井我发现非常有用理解应用程序的流量(如果正确完成)。我常常通过,我就不会在调试器中发现的日志中寻找发现的问题。 IE浏览器。在多个类(尤其是在UI类),奇数据问题不会导致真正的错误,等等。在使用调试这些情况下不必要的code执行无异是想用勺子铺设混凝土。在另一方面调试器是完美的分析由伐木突出问题的细节。

所以在我的code我倾向于有其目的是告诉我是什么应用程序在做,在英语类似的方式,所以我可以轻松地阅读和理解所发生的事情记录了相当数量。但我相当严格的保守水平尽可能低。 IE浏览器。在信息级别不记录的东西,除非它是你想看到即使是在释放模式的东西。我发现了很多的开发人员往往打破了这个。

更新:只是读<一个href=\"http://stackoverflow.com/questions/2446248/deactivate-any-calls-to-log-before-publishing-are-there-tools-to-do-this\">Deactivate任何调用之前发布日志:是否有工具来做到这一点其中谈到了如何使用ProGuard的从发行code脱衣记录?伟大的想法,意味着你不能与日志记录后卫块懒得和你喜欢把尽可能多的,但仍然放心,您的发行code将快速。

I'm an old school developer (OK, I'm 20, I'm not old school, I just like printing it out instead of using the step-by-step debugger :P ) and I have a lot of Log.i() calls in an Android application. I was wondering if it would have an impact on the performance of the app?

I know I should use a step-by-step debugger, it's just that debugging multiple threads can be a little cumbersome.

Thanks for your help!

解决方案

I'm still looking into Android coding myself but I from what I've seen so far I would assume that the Android logging suffers from the same issues that most Java logging frameworks suffer from (The notable exception being SLF4J), namely:

  • Logging does effect performance because:

    1. It means extra method calls. (Not an issue for 99% of apps).
    2. It often means String assembly work (Big impact)
    3. It engages extra IO (Big impact)

Developers typically deal with this by adding guard blocks

if (log.isDebugEnabled()) {
   log.debug("...");
}

I think the Android SDK can do this as well. it covers off #2 and #3, but still leaves your release code containing unused logging code. assuming of course that you never want to enable debug logging in a release situation.

SFL4J BTW, doesn't require guard blocks because it used C like method calls which delay String assembly until it's actually required. Unfortunately it appears that Android has not gone down this path. iOS doesn't have this problem either because it has a pre-compiler. Something that I often wish Java had kept.

Anyway, I would not advocate removing logging in favour of the debugger. IMHO they server two different purposes. Logging I find very useful (if done properly) in understanding the flow of an application. I've often found problems by looking through logs that I would not have found in the debugger. Ie. unnecessary code execution across multiple classes (especially in UI classes), odd data issues that don't lead to actually bugs, etc. In these situations using the debugger would be like trying to lay concrete with a spoon. Debuggers on the other hand are perfect for analysing the fine details of an issue highlighted by the logging.

So in my code I tend to have a fair amount of logging which is designed to tell me what the application is doing, in an English like manner so I can read it easily and understand what is happening. But I'm fairly strict about keeping the levels as low as possible. Ie. don't log stuff at the info level unless it's something you want to see even in release mode. I've found a lot of developers tend to break this.

UPDATE: Just read Deactivate any calls to Log before publishing: are there tools to do this? which talks about how you can use ProGuard to strip logging from release code. Great idea, means you can not bother with guard blocks on logging and put as much in as you like, yet still be assured that your release code will be fast.

这篇关于是否Log.i()影响到Android应用程序的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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