2

How can i rewrite this code, to get better performance?

    int i = 0;
    ArrayList<ArrayList> data = new ArrayList<ArrayList>();
    //---- fill data with 2 equally large ArrayLists, 2 table columns here

    for (int n = 0; n < data.get(0).size(); n++) {  //Loop for whole table
       if (i < n){  //not to enter second loop, while in previous second loop
           if (data.get(1).get(n) > 0){  // row with condition when to enter second loop
               for (i = n; i < data.get(0).size(); i++){ //second loop
                  if (data.get(0).get(i) > data.get(0).get(n) + 10 ){ // breaks second loop
                  //somecode
                  break;
                  }
               }
            }
        }
    }

Basically what is does, it goes through table row by row (1st loop), until it finds a first specific "starting" row (where second column is > 0), from this point it looks for another specific "ending" row (2nd loop), until it finds it (value in this row must be at least 10 higher than in starting row) and ends 2nd loop. It will not go into the second loop, if it already is in one. It will look for the starting condition only past the last row with ending condition (the if (i < n) part).

I know it's crude, i made this with highschool visual basic knowledge transformed to java lang.

How can i program this in a better way, to get better performance, as the table/array is really long and i have to go through it a lot of times looking for a different starting condition (perhaps working with db instead of arraylists?, how would a db query look like?)

6
  • I think SQL will only add overhead to this, because the nature of your query is dynamic. The best thing you can do IMO is to try to re-write your code or pre-process it to avoid the "scans" in your structure. Commented Jan 19, 2016 at 13:36
  • No, that was the first i came up with. Basically i want prog. to do what is in explanation and in order, as the input table/data are ordered by time. Commented Jan 19, 2016 at 14:38
  • 1
    Is the data in the list sorted by that field? In this case, you could use binary search to find the value that is 0 and then the value after that that is +10. Commented Jan 19, 2016 at 14:52
  • You could use generics correctly (e.g. ArrayList<ArrayList<Integer>>) and the fact you have two columns and they are anonymous doesn't help the readability. You can make two 1D lists, you can also make a 1D list with some custom object of yours as an entry, it will help you make it more readable for both you and us. Commented Jan 19, 2016 at 16:01
  • As for your problem itself: It seems your algorithm has linear complexity, which is pretty good. You could probably make the code more readable, but it doesn't seem like a bottleneck unless your array is really huge and you perform this operation repeatedly (e.g. after adding a row). Those are however factors we know nothing about, you only described this algorithm. Commented Jan 19, 2016 at 16:05

1 Answer 1

1

Personally I woudn't use ArrayList when having just 2 columns.

You could do something like this:

    class Column{
        Integer first;
        Integer second;
    }

    ArrayList<Column> data = new ArrayList<Column>()

But that shouldn't make much difference. By the way: at last tell, which type your ArrayLists are. Like: ArrayList < ArrayList < Integer > > instead of ArrayList < ArrayList > ).

The if "i < n" is quite unnecessary. You could do something like this:

    for (int n = 0; n < data.get(0).size(); n++) {  //Loop for whole table
           if (data.get(1).get(n) > 0){  // row with condition when to enter second loop
               int n0=n;
               for (; n < data.get(0).size(); n++){ //second loop
                 if (data.get(0).get(n) > data.get(0).get(n0) + 10 ){ // breaks second loop
              //somecode
                 break;
                 }
              }
           }
        }

The java for-loop is really flexible. Java just calls first "parameter" before calling the loop, and last "parameter" after each iteration. And the loop continues till second "parameter" returns False.

So you don't have to skip iterations with if - just increment n and this will also skip loop iterations. If you e. g. set n=10, then java will continue calling the loop iteration for n=10, 11, 12, and so on. If you just also use n as your variabel counter for the second loop, it will do, what you want.

Using database is quite unnecessary.

I don't know exectely what you want to do. Sorting your table before calling your code might open completely new options. If you don't want to sort the table, just iterating element by element should be the only possibility.

Sign up to request clarification or add additional context in comments.

3 Comments

You crippled his algorithm a bit - you aren't setting i anywhere. In the code it is the internal cycle variable while n stays fixed. So I guess you should save the value n to some variable before entering second cycle. Other than that, I guess you did well, noticing that it is in fact linear. (Which is not really bad, so I guess the problem lies somewhere outside the code in question and your answer).
Yes,i now see the i is not necessary, simple continuing with n will do the same,that might save some time. Also right now it is object type array, for historical reasons:p, but might as well make it double which will also save time removing some double.parsedouble from calculations done in the loop. If it will be double, will it open some new opportunities to simplify? And it does not have just 2 columns, maybe up to 20, it is flexible by different part of the code where it is populated. Thanks helpfull tips, will get back if it helped
not directly solved my problem, as the loop is in the end pretty fast, but your remark about ArrayList < ArrayList < Integer > > instead of ArrayList < ArrayList >, made me to change those arraylists to <double> and then removed my double.parsedouble(arraylist.get(object).tostring()) from calculations inside the loop, which were there for historical reasons:P, which made this loop run aprox. 20 times faster:), so lesson learned, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.