0

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);
                           }
                     }
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
Jaggu
  • 67
  • 1
  • 3
  • 8
  • 5
    How do you know this is the bottleneck in your program? Have you run a profiler on it? – OldProgrammer Dec 20 '15 at 15:50
  • 1
    The answers you are getting are guesses, suspicions, generalities. Each one *might* be right. Don't you want to *know* what is costing time? so you can fix it and save a whole lot of those 30 hours? It doesn't cost anything to [*try this*](http://stackoverflow.com/a/378024/23771). – Mike Dunlavey Dec 20 '15 at 16:53
  • Thank you so very much guys (OldProgrammer, Mike Dunlavey ) and sure i will follow your inputs.. – Jaggu Dec 22 '15 at 08:32

5 Answers5

1

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.

ajacian81
  • 7,419
  • 9
  • 51
  • 64
0

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!

assylias
  • 321,522
  • 82
  • 660
  • 783
  • A conditional branch that is not correctly predicted by the hardware takes a lot more than one cycle. The processor has to invalidate its entire pipeline, and begin fetching instructions from the correct address. – Patricia Shanahan Dec 20 '15 at 15:56
  • Fair enough but even though it's unlikely to be the main culprit for the slowness. – assylias Dec 20 '15 at 16:30
0

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 ...

0

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.

Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34
0

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. Initialize the variables in declaration itself. This will handle NullPointerExceptions.

  2. Decide on mutability of member variables early in the cycle. Use language constructs like final keyword effectively.

  3. If you know that augments for method won't be changed, declare them as final.

  4. 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.

  5. And finally, use try{} catch{} finally{} blocks at right places effectively.

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211