0 Replies Latest reply on Aug 30, 2013 12:13 AM by xStardust!

    Copying arrays, performance questions


      Hello there


      The JDK offers several ways to copy arrays so I ran some experiments to try and find out which would be the fastest.



      I was measuring the time it takes to copy large arrays of integers. I wrote a program that allocates arrays of various sizes, and copy them several times using different methods. Then I measured the time each method took using the NetBeans profiler and calculated the frequencies.




      Here are the results I obtained (click for full size):  http://i.share.pho.to/dc40172a_l.png

      (what I call in-place copy is just iterating through the array with a for loop and copying the values one by one)


      I generated a graph from those values:  http://i.share.pho.to/049e0f73_l.png


      A zoom on the interesting part: http://i.share.pho.to/a9e9a6a4_l.png




      According to these results, clone() becomes faster at some point (not sure why). I've re-ran these experiments a few times and it seems to always happen somewhere between 725 and 750.



      Now here are my questions:

      - Is what I did a valid and reliable way to test performances, or are my results completely irrelevant? And if it's not, what would be a smarter way to do this?

      - Will clone be faster than arraycopy past 750 items on any PC or will these results be influences by other factors?

      - Is there a way to write a method that would copy the array with optimal performances using clone and arraycopy, such that the cost of using it would be insignificant compared to systematically using one method over the other?

      - Any idea why clone() can become faster for bigger arrays? I know arraycopy is a native method, I didn't try to look into what it does exactly but I can't imagine it's doing anything more complicating than copying elements from one location in the memory to another... How can another method be faster than that?

      (just reminding I'm copying primitives, not objects)





      Message was edited by: xStardust! Added links, mr forum decided to take away my images