Reputation: 67
This job is talking about 30+ hours of time. 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. 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);
}
}
Upvotes: 0
Views: 104
Reputation: 38930
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.
Avoid lazy initialization
of member variables as much as possible. Initialize the variables in declaration itself. This will handle NullPointerExceptions.
Decide on mutability
of member variables early in the cycle. Use language constructs like final
keyword effectively.
If you know that augments for method won't be changed, declare them as final
.
Limit the mutation
of data as much as possible. Some variables can be created in a constructor and can never be changed. Remove public setter methods if possible.
And finally, use try{} catch{} finally{}
blocks at right places effectively.
Upvotes: 0
Reputation: 21500
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.
Upvotes: 0
Reputation: 3079
Other method: check time !
Use a monitor, like JVM monitor: http://www.jvmmonitor.org/
Install it in Eclipse, and you can verify what really spends time ...
Upvotes: 0
Reputation: 328727
A null check probably takes one CPU cycle at most, let's call it 1ns to be conservative. 800k x 20 x 1 ns = 16 ms...
=> the null checks are not your problem!
Upvotes: 0
Reputation: 7569
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.
The catch block would just continue the loop.
Upvotes: 1