Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimization: replace for loop with ListIterator
    primarykey
    data
    text
    <p>It's my first working on a quite big project, and I've been asked to obtain the best performances.</p> <p>So I've thouhgt to replace my for loops with a <code>ListIterator</code>, because I've got around 180 loops which call <code>list.get(i)</code> on lists with about 5000 elements.</p> <p>So I've got two questions.</p> <p>1) Are those 2 snippets equal? I mean, do them produce the same output? If no, how can I correct the <code>ListIterator</code> thing?</p> <pre><code>ListIterator&lt;Corsa&gt; ridesIterator = rides.listIterator(); while (ridesIterator.hasNext()) { ridesIterator.next(); Corsa previous = ridesIterator.previous(); //rides.get(i-1) Corsa current = ridesIterator.next(); //rides.get(i) if (current.getOP() &lt; d.getFFP() &amp;&amp; previous.getOA() &gt; d.getIP() &amp;&amp; current.wait(previous) &gt; DP) { doSomething(); break; } } </code></pre> <p>__</p> <pre><code>for (int i = 1; i &lt; rides.size(); i++) { if (rides.get(i).getOP() &lt; d.getFP() &amp;&amp; rides.get(i - 1).getOA() &gt; d.getIP() &amp;&amp; rides.get(i).getOP() - rides.get(i - 1).getOA() &gt; DP) { doSomething(); break; } } </code></pre> <p>2) How will it be the first snippet if I've got something like this? (changed i and its exit condition)</p> <pre><code>for (int i = 0; i &lt; rides.size() - 1; i++) { if (rides.get(i).getOP() &lt; d.getFP() &amp;&amp; rides.get(i + 1).getOA() &gt; d.getIP() &amp;&amp; rides.get(i).getOP() - rides.get(i + 1).getOA() &gt; DP) { doSomething(); break; } } </code></pre> <p>I'm asking because it's the first time that I'm using a <code>ListIterator</code> and I can't try it now!</p> <p>EDIT: I'm not using an ArrayList, it's a custom List based on a LinkedList</p> <p>EDIT 2 : I'm adding some more infos. I can't use a caching system because my data is changing on evry iteration and managing the cache would be hard as I'd have to deal with inconsistent data. I can't even merge some of this loops into one big loop, as I've got them on different methods because they need to do a lot of different things.</p> <p>So, sticking on this particular case, what do you think is the best pratice? Is ListIterator the best way to deal with my case? And how can I use the ListIterator if my for loop works between 0 and size-1 ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload