简体   繁体   English

对我来说,做这些空检查并改善此代码的性能的更好方法是什么?

[英]What is a better way for me to do these null checks and improve the performance of this code

This job is talking about 30+ hours of time. 这项工作正在谈论30多个小时的时间。 This Iterator holds 800k records and the loop has to execute for each record and there are about 20+ of these checks in the same method. 该迭代器可容纳80万条记录,并且循环必须为每条记录执行,并且在同一方法中大约有20多个这些检查。 How can I avoid the cyclomatic complexity here? 在这里如何避免圈复杂度?

CustomerPreference newRecord = null;

while (iterator.hasNext())
              {

final CustomerPreference current = iterator.next();

                     if (newRecord != null)
                     {
                           if (!newRecord.getEmail().equals(current.getEmail()))
                           {
                                  resList.add(newRecord);
                                  newRecord = current;
                           }
                     }
                     else
                     {
                           newRecord = current;
                     }

                     if (null != current.getEmailPref())
                     {
                           newRecord.setEmailPref(current.getHdCaEmailPref());
                     }

              else
                     {
                           if (newRecord.getEmailPref() == null)
                           {
                             newRecord.setEmailPref(NO_CHANGE);
                           }
                     }

If this is just an internal app, take out the checks and enclose your code in a try/catch block, especially if the likelihood of a null reference is slim. 如果这只是一个内部应用程序,请进行检查并将代码包含在try / catch块中,尤其是在空引用的可能性很小的情况下。

The catch block would just continue the loop. catch块将继续循环。

A null check probably takes one CPU cycle at most, let's call it 1ns to be conservative. 空检查最多可能需要一个CPU周期,为了保守起见,我们将其称为1ns。 800k x 20 x 1 ns = 16 ms... 800k x 20 x 1 ns = 16毫秒...

=> the null checks are not your problem! =>空检查不是您的问题!

Other method: check time ! 其他方法:检查时间!

Use a monitor, like JVM monitor: http://www.jvmmonitor.org/ 使用监视器,例如JVM监视器: http : //www.jvmmonitor.org/

Install it in Eclipse, and you can verify what really spends time ... 将其安装在Eclipse中,您可以验证真正花费时间的内容...

I suspect that the real performance problem is outside of the shown code. 我怀疑真正的性能问题不在显示的代码范围内。 Even if a string compare takes a microsecond and an iteration of the loop contains hundreds of them, a single iteration should not take longer than about a millisecond. 即使字符串比较花费一微秒的时间,并且循环的迭代包含数百个微秒,一次迭代的时间也不应超过一毫秒。 And that should result in a runtime of about 10 to 15 minutes. 这将导致大约10到15分钟的运行时间。

Those checks may not be the reason for performance degradation. 这些检查可能不是性能下降的原因。 But you can avoid possible logical errors by following good coding conventions. 但是您可以通过遵循良好的编码约定来避免可能的逻辑错误。

I am following below conventions. 我遵循以下约定。

  1. Avoid lazy initialization of member variables as much as possible. 尽可能避免成员变量的lazy initialization Initialize the variables in declaration itself. 在声明本身中初始化变量。 This will handle NullPointerExceptions. 这将处理NullPointerExceptions。

  2. Decide on mutability of member variables early in the cycle. 在周期的早期确定成员变量的mutability Use language constructs like final keyword effectively. 有效地使用诸如final关键字之类的语言结构。

  3. If you know that augments for method won't be changed, declare them as final . 如果您知道方法的扩充不会更改,请将其声明为final

  4. Limit the mutation of data as much as possible. 尽可能限制数据的mutation Some variables can be created in a constructor and can never be changed. 某些变量可以在构造函数中创建,并且永远不能更改。 Remove public setter methods if possible. 如果可能,请删除公共设置方法。

  5. And finally, use try{} catch{} finally{} blocks at right places effectively. 最后,在正确的位置有效地使用try{} catch{} finally{}块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM