Why is the ArrayList.iterator() so slow

Take a look at the code below. I believe its correct. If so, I can get 3x the performance doing a classic for loop when iterating the list.
public static void main(String[] args)
          List<Double> theList = new ArrayList<Double>(LIST_LENGTH);
          //Only run last test with Linked List
          //List<Integer> theList = new LinkedList<Integer>();
          Random rand = new Random();
          System.out.print("Populating list with " + LIST_LENGTH + " items.");
          for (int i = 0; i < LIST_LENGTH; i++)
               theList.add(rand.nextDouble());
          System.out.println("Done!");
          //new Zebra(theList);
          System.out.println("Classic For Loop");
          double t1 = 0;
          long start = System.currentTimeMillis();
          for (int i = 0; i < LIST_LENGTH; i++)
               t1 += theList.get(i);
          long stop = System.currentTimeMillis();
          System.out.println("Total1: "  + t1 + " Time: "+  + (stop - start));
          System.out.println("New For Each Loop");
          double t2 = 0;
          long start2 = System.currentTimeMillis();
          for (double f : theList)
               t2 += f;
          long stop2 = System.currentTimeMillis();
          System.out.println("Total2: " + t2 + " Time: "+ (stop2 - start2));
          System.out.println("Iterator based loop");
          double t3 = 0;
          long start3 = System.currentTimeMillis();
          for (Iterator<Double> it = theList.listIterator(); it.hasNext();)
               t3 += it.next();
          long stop3 = System.currentTimeMillis();
          System.out.println("Total3: " + t3 + " Time: "+ (stop3 - start3));
     }Here is a print screen of a run. These results are typical. It does not matter which order I run the test.
Populating list with 20000001 items.Done!
Classic For Loop
Total1: 9999145.376459487 Time: 375
New For Each Loop
Total2: 9999145.376459487 Time: 1079
Iterator based loop
Total3: 9999145.376459487 Time: 1063Anyone running this will need to get their -Xmx parameter up fairly high.

I expect that's the case. There are times though, like in this sample, when there is no chance the list will be modified by another thread. I guess this is just a lesson "knowing your language." I discovered this when testing a multiple thread sumation. For the curious I have posted the rest of the class below. It would be interesting to see the results on an Core i7 processor.
One thing this does show is that "multi-threaded" is not necessairly the best way to do things. I have to have a very large collection before the "Divide and conquor" approach shows any benefit. Test relatively similar performance until I get to a collection of 2,000,000 or more objects.
This test also shows how inaccurate even dobule percision floating point math can be. If I change the items in the lists to ints the numbers will all match. There cases when using doubles will match too. Ex: Instead of using random numbers I just use "1". In this case using floats will be different but using doubles will give the right answer.
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;
public class Zebra
     private final Set<xThread> runningThreads = new HashSet<xThread>();
     private float total = 0;
     private static final int LIST_LENGTH = 50000000;
     private static final int NUM_THREADS = Runtime.getRuntime().availableProcessors();
     public Zebra(final List<Double> theList)
          final int LIST_LENGTH = theList.size();
          final int paddingNeeded = (LIST_LENGTH % NUM_THREADS) > 0 ?
                                       NUM_THREADS - (LIST_LENGTH % NUM_THREADS) : 0;
          final int effectiveLength = LIST_LENGTH + paddingNeeded;
          System.out.println("Adding padding " + paddingNeeded);
          for (int i = 0; i < paddingNeeded; i++)
               theList.add(0d);
          // Split job into threads
          System.out.println("Divide and Conquor (note this test is counting overhead of setting up threads)");
          long start = System.currentTimeMillis();
          synchronized (this)
               for (int i = 0; i < NUM_THREADS; i++)
                    xThread x = new Zebra.xThread(theList,
                                                         (effectiveLength / NUM_THREADS) * i,
                                                         (effectiveLength / NUM_THREADS) * (i + 1), this);
                    runningThreads.add(x);
                    x.start();
               while (runningThreads.size() > 0)
                    try
                         wait();
                    catch (InterruptedException e)
                         e.printStackTrace();
          if (paddingNeeded > 0)
               for (int i = 0; i < paddingNeeded; i++)
                    theList.remove(theList.size() - 1);
          long stop = System.currentTimeMillis();
          System.out.println("Total: " + total + " Time: " + (stop - start));
     private void threadDone(xThread x)
          synchronized (this)
               runningThreads.remove(x);
               notifyAll();
               total += x.getTotal();
     private class xThread extends Thread
          private final List<Double> list;
          private final int startIndex, endIndex;
          private final Zebra backRef;
          private double total = 0;
          public xThread(List<Double> list, int startIndex, int endIndex,
                    Zebra backRef)
               this.list = list;
               this.startIndex = startIndex;
               this.endIndex = endIndex;
               this.backRef = backRef;
          public double getTotal()
               return total;
          public void run()
               for (int i = startIndex; i < endIndex; i++)
                    total += list.get(i);
               backRef.threadDone(this);
     public static void main(String[] args)
          List<Double> theList = new ArrayList<Double>(LIST_LENGTH);
          //Only run last test with Linked List
          //List<Integer> theList = new LinkedList<Integer>();
          Random rand = new Random();
          System.out.print("Populating list with " + LIST_LENGTH + " items.");
          for (int i = 0; i < LIST_LENGTH; i++)
               theList.add(rand.nextDouble());
          System.out.println("Done!");
          new Zebra(theList);
          System.out.println("Classic For Loop");
          double t1 = 0;
          long start = System.currentTimeMillis();
          for (int i = 0; i < LIST_LENGTH; i++)
               t1 += theList.get(i);
          long stop = System.currentTimeMillis();
          System.out.println("Total1: "  + t1 + " Time: "+  + (stop - start));
          System.out.println("New For Each Loop");
          double t2 = 0;
          long start2 = System.currentTimeMillis();
          for (double f : theList)
               t2 += f;
          long stop2 = System.currentTimeMillis();
          System.out.println("Total2: " + t2 + " Time: "+ (stop2 - start2));
          System.out.println("Iterator based loop");
          double t3 = 0;
          long start3 = System.currentTimeMillis();
          for (Iterator<Double> it = theList.listIterator(); it.hasNext();)
               t3 += it.next();
          long stop3 = System.currentTimeMillis();
          System.out.println("Total3: " + t3 + " Time: "+ (stop3 - start3));
}Results:
Populating list with 50000000 items.Done!
Adding padding 0
Divide and Conquor (note this test is counting overhead of setting up threads)
Total: 2.499794E7 Time: 656
Classic For Loop
Total1: 2.499794097321981E7 Time: 954
New For Each Loop
Total2: 2.499794097321981E7 Time: 3080
Iterator based loop
Total3: 2.499794097321981E7 Time: 2658

Similar Messages

  • Why is the itunes download so slow

    why is the itunes download so slow - I am trying to download itunes update to recognize my new ipod touch 5th generation and my pc will not even show it in itunes, in trying to download the update the speeds are so slow that it is estimating download time in days.  I have stopped and started the apple in services, but it is still unrecognized.

    you know very well it has nothing to do with itunes or the modem/router.  apple servers are very very slow and its an absolute disgrace.
    you can download from third party servers at twice the speeds (at your own risk)
    Apple is the second largest company in the world and they treat customners in this way

  • Why is the wi-fi so slow??

    Why is the wi-fi so slow on the time capsule??? Brand new bought last week? help? running about 13 devices on more than one network. Help please..

    Is it slow compared to other devices.. ??
    Give numbers.. cannot operate from general comments.
    Is this 2.4ghz.. are you splitting the load over 2.4ghz and 5ghz??
    Have you tried using SMB names.. short no spaces pure alphanumeric, with different name for 5ghz.
    Lock the wireless channel for 2.4ghz and test 1, 6, 11 in turn.
    Using WPA2 personal security only.. with 8-12 character passkey.. pure alphanumeric of course.

  • Why is the On Demand so slow to respond

    Title says it all
    Why is the On Demand so slow to respond?
    You have very fast Internet but extremely On Demand Programming Why is that?
    Fix it or ill have to go back to Road Runner. My wife runs a Preschool/Daycare and needs Fast On Demand programming because she's very busy with the kids.
    She allows them to watch some of the children's programming if they did well in the class work but she doesn't have time to wait for the EXTREMELY SLOW Response.

    Memory leakage causes it to run slow. One way to easily fix this is to reboot the box. It will automatically reallocate memory. To reboot, simply unplug power cord for 1 min then plug back in.
    Anthony_VZ
    **If someones post has helped you, please acknowledge their assistance by clicking the red thumbs up button to give them Kudos. If you are the original poster and any response gave you your answer, please mark the post that had the answer as the solution**
    Notice: Content posted by Verizon employees is meant to be informational and does not supersede or change the Verizon Forums User Guidelines or Terms or Service, or your Customer Agreement Terms and Conditions or plan

  • Why is the new firefox so slow hate it ?

    Why is the new Firefox so slow really hate i wish they would fix it ?

    Try Google Chrome. That one is really fast.

  • Why is the sound browser so slow during playback?

    Why is the sound browser so slow during playback? This problem seems to have no solution. This problem even occurs on my Mac Pro With 8 Cores... This problem is in its 3rd Generation since version 8... Even AU Plugins with built-in sound browsers trigger sounds in real time. Whats up Apple? I would love to find a solution after 5 yrs of using this program... U-Jean

    This Problem has been occurring before 2011 according to the link below....... *** Apple!!!
    http://www.logicprohelp.com/forum/viewtopic.php?t=70175

  • Why does the videos looks so slow

    why does the videos looks so slow only in the internet

    Many reasons, like what format they are, for instance Flash Videos will be slower.
    Any links to the slow ones?
    Which exact Mac do you have?

  • Why has the download/buffering speed slowed down for videos?

    Why has the download/buffering speed slowed down for videos?
    What can be done about it?
    TIA,
    R. Ober

    The problem is with your router, your internet service provider or you have a marginal connection with your router.
    Locate your iPad close to your wifi router and see if the problem persists.
     Cheers, Tom

  • Why is the write speed so slow to my NAS

    Hi, I am trying to improve the write speed from my MBP to the NAS.  I am using the Thunderbolt ethernet adapter connected to an Airport Extreme 7.6.1 using a wired connection. I have a Synology DS213 with a wired connection to the Airport Extreme as well. I am using CAT6 cables.
    In testing my performance, I noticed writing files from my MBP to the NAS was very slow; whereas my read performance was much better.  Please see the tests below to explain this further.
    Using the Blackmagicdesign Disk Speed Test Utility using a 5Gb test file written to the NAS, I see the following:
    Write speed is 7 to 8MB/s  This write speed is what concerns me.
    Read speed is between 60 MB/s and 75 MB/s
    Using 2 finder windows (drag a file from one window(MBP) to the other(NAS)) and watch the disk activity on the Activity Monitor with a 1Gb file
    When writing a file to the NAS from the MBP:
    Write speed will burst to 19 to 23 MB/s but it will drop to below 10 MB/s at times.
    When writing a file to the MBP from the NAS
    Write speed is: 46 to 60 MB/s
    Why would the write speed from my MBP to the NAS be so slow ; whereas, the write speed from the NAS to the MBP is very fast.
    Synology has 2 WB 'green' disks configured as raid 1.   I understand that part of the influence of writing from the NAS to the MBP is utilzing the advantage of writing to a SSD ; whereas, writing to the NAS it is writing to a slower drive.  
    Is 19 to 23 MB/s burst with dips below 10 MB/s during a copy typical?   The other thing is I dont understand why the Disk Speed Utility never records write speeds to the NAS over 10 MB/s whereas the Activity Monitor will show the bursts at times of 19 to 23 MB/s
    If 7 to 8 MB/s write speeds is too slow, what can I look at to improve this?  Since writing the file in the other direction is pretty fast considering, how could the issue be a network one?

    I was transferring my iPhoto library from an older MBP to a new retina and having the same issue - transfers almost immediately slowing to less than 100k/sec over my beautiful Time Warner-provided WiFi router using regular OSX folder sharing.  I couldn't find any software updates as some other similar threads suggested.
    I put my iPhoto library in a .tar.gz archive and that immediately transfered at about 7mb/sec, much much faster!  So, without having diagnosed the issue, perhaps I've uncovered evidence that one or both of my machines doesn't like transferring big file trees.  I bet a zip or any other archive would do the trick.

  • Why is the Space Constant so slow?

    Obviously I'm asking comparatively since they are faster then the blink of an eye.
    When trying to optimize some of my functions I've noticed that this one constant is really slow.
    Here's my test VI: I simply am measuring the time it takes to append a character to a string. If the string doesn't exist, don't add anything. This also prevents the compiler from optimizing out the for loop.
    Most cases I've averaged about 250 ms. However, if I used the space constant instead of putting a space character in a string constant. It nearly 10-fold the time it takes to nearly 2.1 seconds.
    So I question why is this the case? I don't see why all the other constants (I checked) run at full speed, but the space constant (which I can see it's block diagram) takes significantly longer. Is this a bug?
    Attachments:
    Space Timing.vi ‏14 KB

    bsvare wrote:
    As for being flawed. I wanted it to be. I don't want my for loop to be folded away. This simple test VI is so simple that it takes literally 10 million runs to have reasonible run time for compairison. If you fold it away to a single run, you can't see the effects.
    Well, it is flawed in the wrong way, because most likely the presence of a subVI will cause more complicated debugging code and the effect might be secondary. You could e.g. eliminate folding by e.g. doing a comparison of "[i] != 0" or "[i] AND 1" for the case decision. Now all variantions seem to be about the same speed (with or without debugging, resp., where debugging poses a large penalty).
    LabVIEW Champion . Do more with less code and in less time .

  • Why is the SMS app so slow to load?

    Is it just me or is the SMS app really slow to load?
    7 seconds seems a lot to me.
    How is it for u?
    How can I improve the loading time?
    Thank you

    I don't know why Apple doesn't fix this problem or even acknowledge it. I have had this problem since 1.1.2. Upgrading through 1.1.3, 1.1.4 and on the 2.1 has not helped. My SMS loading time varies between 3 and 10 seconds. I maintain all my SMS messages deleted (which I should not have to do). Due to lack of support from Apple I purchased a Nokia phone for my son. At least basic phone features work like they should.

  • Why is the IPad 4 so slow to charge and fast to discharge ??

    Why does the IPad 4 discharge so fast and be so slow to recharge

    Hello, willismom. 
    Thank you for visiting Apple Support Communities. 
    Battery life can be difficult to troubleshoot as multiple factors contribute to how long a charge can last.  Graphic intensive applications, display brightness and data connectivity are just a few of the items that can cause the battery life to decrease rapidly.  I would need to know a little more information about your usage patterns to give you and idea on how to maximize battery life.  However, here are some tips on how to adjust settings and charge the device that may help. 
    iPad: Charging the battery
    http://support.apple.com/kb/ht4060
    About Batteries
    About iPad Batteries
    If you feel that you need assistance with the battery concern, you can always reach out to us via the link below. 
    Contact Apple
    Cheers,
    Jason H. 

  • Why is the app store so SLOW?

    Whenever I bring up the App Store application, it takes ages to load.  I just closed App Store and started it again, and it took a full TWO MINUTES for "featured" items to come up. Whenever I try to download something, it takes way longer than any other major download site I use.
    I live in Japan, use the Japan App Store, and am on a 30mbs connection.  So why does the App Store take so long to load and download?

    Has it always been like that?

  • Why is the USB download soooooo slow?  I see this frequently on other forums.  What's the fix?

    I am trying to copy the bootcamp utilities to a usb drive so that windows device drivers can be updated.  The download is very slow - in fact, it looks like its halted.  Its been running for 2 hours.   Is there a work-around for this?  An alternative site to ge the file?  

    Assuming you need the BootCamp 5.Drivers
    Just download and unzip the file  at http://support.apple.com/downloads/DL1638/en_US/BootCamp5.0.5033.zip which is Boot Camp Support Software 5.0.5033
    Burm the contents to a DVD or Flash Drive
    Then insert the DVD or Flash Drive in Windows and run setup as an administrator.

  • HT1338 why would the internet be so slow?

    The internet is running slow on the computer....not opening as quickly and won't stream any media when I need to what could be slowing it down and is there a way to fix it? I haven't had the computer for that long.

    In the Sustem/Network/Advanced/DNS preference pane check to see what DNS servers you're using.  If it's not one of the two below delete those that are there and add both of the two below.  See if that improves performance.  Note what servers were there in case you want to go back to them.
    208.67.222.222
    208.67.220.220
    OT

Maybe you are looking for

  • How do i find my backed up ipad files on my computer ?

    my ipad was restriced by my little cousin cause he put in a password on the iPad and disabled my Apps Store app, can someone tell me how to retrieve the Restriction Code without restoring the iPad ?

  • Flat file to Oracle: Dynamic flat file name

    Hi, I am doing a transformation from flat file to oracle. At one place there is requirement like flat file name is dynamic. *eg. D:\ Flat DB\ Sample<Date>.txt    examples may be like Sample09012012,Sample08012012 * As in physical schema we specify na

  • (urgent) Can't Start the database

    Hi, can anybody help me ? i shutdown the database on Sun solaris from telnet. On the middle the internet connection wa down. And when i connect again and try to start the database there is a error ORA-27146: post/wait initialization failed i try to s

  • CC: Fatal error in ccfe: Bus Error (core dumped)

    I'm trying to compile a file that's heavy on templates (both mine and boost's). After some time I get the following error: CC: Fatal error in ccfe: Bus Error (core dumped)ccfe consumes up to 390 mb aprox. What could be the cause of this? Could it be

  • Can't Synch to the new Itunes

    I got the older 15 gig Ipod. I downloaded the new itunes and like it but it wont synch properly. I connect my ipod and let it run the course but when it finishes synching the new songs I imported aren't there. Anybody know what i'm doing wrong?