While somehow contour

Of course, this is not possible in java (currently), but ideally I would like to implement it, since it underlies many iterations. For example, the first time it is called, I do it 650,000+ times when it creates an ArrayList . Unfortunately, the reality is that my actual code does not have set inside the else loop; this way it will pass add commands and then set and waste time.

After that, I have this also in another loop, where it only performs a set, since the data is already created, and it is multi-user in many others, so this is a lengthy process.

 ArrayList<Integer> dataColLinker = new java.util.ArrayList<Integer>(); ... ... public void setLinkerAt( int value, int rowIndex) { ... while(rowIndex >= dataColLinker.size()) { dataColLinker.add(value); } else { dataColLinker.set(rowIndex, value); } 

Any ideas or theories? I'm not sure about speed in java when it comes to if and ArrayList if , etc.

+10
java performance arraylist theory while-loop
source share
7 answers

Did I miss something?

Not this hypothetical code

 while(rowIndex >= dataColLinker.size()) { dataColLinker.add(value); } else { dataColLinker.set(rowIndex, value); } 

mean the same thing as this?

 while(rowIndex >= dataColLinker.size()) { dataColLinker.add(value); } dataColLinker.set(rowIndex, value); 

or that?

 if (rowIndex >= dataColLinker.size()) { do { dataColLinker.add(value); } while(rowIndex >= dataColLinker.size()); } else { dataColLinker.set(rowIndex, value); } 

(The latter makes more sense ... I think). In any case, it is obvious that you can rewrite the loop so that the "else test" does not repeat inside the loop ... as I just did.


FWIW, this is most likely a case of premature optimization. That is, you probably spend time optimizing code that does not need to be optimized:

  • As far as you know, the JIT compiler optimizer may already have moved the code so that the "else" part is no longer in the loop.

  • Even if this is not the case, it is likely that the particular thing you are trying to optimize is not a significant bottleneck ... even if it can be performed 600,000 times.

My advice is to forget this problem. Run the program. When it works, decide whether it will work fast enough. If this is not the case, profile it and use the profiler output to decide where it is worth spending your time optimizing.

+20
source share

I do not understand why there is some encapsulation ...

Using

 //Use the appropriate start and end... for(int rowIndex = 0, e = 65536; i < e; ++i){ if(rowIndex >= dataColLinker.size()) { dataColLinker.add(value); } else { dataColLinker.set(rowIndex, value); } } 
+3
source share

Wrap the "set" statement so that it means "set if not set" and set it to be naked over the while loop.

You are right, the language does not provide what you are looking for exactly in this syntax, but this is because there are programming paradigms similar to the one I just proposed, therefore you do not need the syntax that you offer.

+1
source share
 boolean entered = false, last; while (( entered |= last = ( condition ) )) { // Do while } if ( !entered ) { // Else } 

Welcome.

+1
source share

Java does not have this control structure.
It should be noted, however, that other languages. For example, Python has a while-else construct .

In the case of Java, you can imitate this behavior, as you have already shown:

 if (rowIndex >= dataColLinker.size()) { do { dataColLinker.add(value); } while(rowIndex >= dataColLinker.size()); } else { dataColLinker.set(rowIndex, value); } 
0
source share

This while statement should only execute else code when the condition is false, which means it will always execute it. But there is a trick when you use the break keyword in a while loop, the else statement should not be executed.

Code that satisfies the condition is only:

 boolean entered = false; while (condition) { entered = true; // Set it to true stright away // While loop code // If you want to break out of this loop if (condition) { entered = false; break; } } if (!entered) { // else code } 
0
source share

Assuming you come with Python and accept this as the same thing:

 def setLinkerAt(value, rowIndex): isEnough = lambda i: return i < dataColLinker.count() while (not isEnough(rowIndex)): dataColLinker.append(value) else: dataColLinker[rowIndex] = value 

The most similar that I could come up with was:

 public void setLinkerAt( int value, int rowIndex) { isEnough = (i) -> { return i < dataColLine.size; } if(isEnough()){ dataColLinker.set(rowIndex, value); } else while(!isEnough(rowInex)) { dataColLinker.add(value); } 

Pay attention to the need for logic and reverse logic. I'm not sure if this is a great solution (duplication of logic), but the unconditional else is the closest syntax I could come up with while maintaining the same act that didn't execute the logic more than was required.

0
source share

All Articles