0

I have code in Java that has an outer loop and several nested loops. The performance is not satisfactory. What strategies can I follow to improve overall performance?

For(int I = 0; I < 20000000; < i++) // 20 million iterations in outer loop
{    
    For(…)

    For(…)

    For(…)

    method...

    method...    
} 
Eric J.
  • 147,927
  • 63
  • 340
  • 553
Nick Ren
  • 65
  • 3
  • 10
  • 1
    Not without giving us more details; there's really almost nothing that can be said based on the amount of detail you've provided. – Louis Wasserman Jun 28 '12 at 18:55
  • I'm going to buck the trend and vote to reopen because this is not `only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet`. On the contrary, **many junior programmers may have this exact question**. Is it a basic question? YES. Is there a duplicate on SO this should be closed in reference to? Maybe. But it there is, close it for the right reason. – Eric J. Jun 28 '12 at 19:08

4 Answers4

4

You are leaving out a lot of detail. However...

If the things that the 4 different for loops do don't have to happen in order, you could run each for loop on a separate thread.

If you have more than 1 CPU core running the code, overall performance will improve.

Other than that, the best way to know if you can improve performance is to see where time is being spent. Run your code in a profiler.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • Hi, my working space provide 12 computers with linux system and each one has 96 GB RAM and 32 Core CPU. Is there any ways to improve the speed with hardware? – Nick Ren Jun 28 '12 at 20:00
  • Absolutely. Run multiple threads for anything that can be processed in parallel, and distribute the work between the various computers. You will need to provide quite a bit more detail about the nature of your work to get advice on splitting the work among multiple computers. – Eric J. Jun 28 '12 at 21:36
0

It's hard to tell without knowing the details :-) Running a for loop shouldn't take so long.

If you have small operations, you should do some microbenchmarking (see here and here).

If you making a bit sophisticated stuff, you need to make some profiling (Eclipse, NetBeans comes with its own profiler, you can also try JProfiler but there are quite a lot of alternatives).

rlegendi
  • 10,466
  • 3
  • 38
  • 50
0

If you can parallelize the loop you can run it on multiple cores. Or you can buy a faster CPU.

Garrett Hall
  • 29,524
  • 10
  • 61
  • 76
0

I have improved performance in C# for loops with yield return. I think you should definitely evaluate using it.

Check this SO question that indicates how to use it in Java.

This are the direct links to the Java Library - from the answer -:

  1. Article
  2. Library
Community
  • 1
  • 1
Cacho Santa
  • 6,846
  • 6
  • 41
  • 73
  • His code is in Java. How does advice about C# help? Also, `yield return` is *not* always the fastest way to do things. It depends on what you are doing. – Eric J. Jun 28 '12 at 19:11
  • @EricJ. That is why I put the Java Link library.... Totally agree that yield returns is not always the fastest way to do things, but sometimes it could really help. – Cacho Santa Jun 28 '12 at 19:14
  • 1
    I missed the Java link. Thanks for bolding it :-) Still, this will only help when the loop would otherwise return some sort of collection when in fact the code processing the objects in that collection only needs them one at a time (avoids memory and time cost of creating a collection just to hold the return value, which is not otherwise needed). Don't see anything in the OP's post that indicates he's doing that, but then, the post is very short on detail. – Eric J. Jun 28 '12 at 19:19