java Array assignment slowly than List.add() -


package test;  import java.util.arraylist; import java.util.list;  public class arrayandlist {     public static void main(string[] args) {         int num = 10000000;         integer[] marray = new integer[num];         list<integer> mlist = new arraylist<>(num);          // array init test         long icurr = system.currenttimemillis();         (int = 0; < num; i++) {             marray[i] = i;         }         system.out.println("array init:" + (system.currenttimemillis() - icurr));          // list init test         icurr = system.currenttimemillis();         (int = 0; < num; i++) {             mlist.add(i);         }         system.out.println("list init:" + (system.currenttimemillis() - icurr));          // array test         long mextimestamp = system.currenttimemillis();         long r1 = 0;         (int = 0; < num; i++) {             r1 += marray[i];         }         system.out.println("array get:" + (system.currenttimemillis() - mextimestamp));          // list test         mextimestamp = system.currenttimemillis();         long r2 = 0;         (int = 0; < num; i++) {             r2 += mlist.get(i);         }         system.out.println("list get:" + (system.currenttimemillis() - mextimestamp));         if (r2 == r1) {             system.out.println("correct");         } else {             system.out.println("error");         }     } } 

result:

array init:3312 list init:3029 array get:19 list get:23 correct 

after several times test , list init faster array assignment. why ? why assignment slower add()? know list composed of array ? why can faster array on assignment.

improved:

package test;  import java.util.arraylist; import java.util.list;  public class arrayandlist {     public static void main(string[] args) {         int num = 10000000;         integer[] marray = new integer[num];         list<integer> mlist = new arraylist<>(num);          // array init test         long icurr = system.currenttimemillis();         (int = 0; < num; i++) {             marray[i] = i;         }         system.out.println("array init:" + (system.currenttimemillis() - icurr));          // list init test         icurr = system.currenttimemillis();         (int = 0; < num; i++) {             mlist.add(i);         }         system.out.println("list init:" + (system.currenttimemillis() - icurr));          mlist.clear();         // second init list         icurr = system.currenttimemillis();         (int = 0; < num; i++) {             mlist.add(i);         }         system.out.println("second list init:" + (system.currenttimemillis() - icurr));          // second init array         icurr = system.currenttimemillis();         (int = 0; < num; i++) {             marray[i] = i;         }         system.out.println("second array init:" + (system.currenttimemillis() - icurr));          // array test         long mextimestamp = system.currenttimemillis();         long r1 = 0;         (int = 0; < num; i++) {             r1 += marray[i];         }         system.out.println("array get:" + (system.currenttimemillis() - mextimestamp));          // list test         mextimestamp = system.currenttimemillis();         long r2 = 0;         (int = 0; < num; i++) {             r2 += mlist.get(i);         }         system.out.println("list get:" + (system.currenttimemillis() - mextimestamp));         if (r2 == r1) {             system.out.println("correct");         } else {             system.out.println("error");         }     } } 

result:

array init:3331 list init:2867 second list init:725 second array init:814 array get:20 list get:25 correct 

after improved test method , test show more interesting , confusing result. assignment , add() both more faster. assignment still slower assignment.

array assignment or list.add takes no time in case. thing benchmarking garbage collector. scanning through 40 million objects going take lot of time.

garbage collector unpredictable. don't known when full gc triggered , when heap size increased. time differences effect of that. if more passes, see no difference between list , array.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -