can just write a test code , the result is not right ah. .
ArrayList:
long startTime = System.currentTimeMillis();
List<String> arrayList = new ArrayList<String>();
for(int i=0;i<100000;i++){
arrayList.add("a"+i);
}
long endTime = System.currentTimeMillis();
System.out.println("arrayList:"+arrayList.size()+"---"+(endTime - startTime));
print result is arrayList :100000 --- 39
LinkedList:
long startTime1 = System.currentTimeMillis();
List<String> linkedList = new LinkedList<String>();
for(int i=0;i<100000;i++){
linkedList.add("a"+i);
}
List<String> arrayLinkedList = new ArrayList<String>(linkedList);
long endTime1 = System.currentTimeMillis();
System.out.println("arrayLinkedList:"+arrayLinkedList.size()+"---"+(endTime1 - startTime1));
print result is arrayLinkedList :100000 --- 68
why the following slower it ? The following open only in the last one contiguous memory 100,000 . The above should be opened each loop current cycles of memory. . How fast one should be more open . Open memory is not a very time-consuming thing?
------ Solution ---------------------------------------- ----
long startTime1 = System.currentTimeMillis();
List<String> linkedList = new LinkedList<String>();
for(int i=0;i<100000;i++){
linkedList.add("a"+i);
}
long endTime1 = System.currentTimeMillis();
List<String> arrayLinkedList = new ArrayList<String>(linkedList);//这步后移就没问题了,这里进行了一次遍历
System.out.println("arrayLinkedList:"+arrayLinkedList.size()+"---"+(endTime1 - startTime1));
------ Solution ------------------------------ --------------
I use this method to test :
private static void test(int count) {
long startTime = System.currentTimeMillis();
List<String> arrayList = new ArrayList<String>();
for (int i = 0; i < count; i++) {
arrayList.add("a" + i);
}
long endTime = System.currentTimeMillis();
System.out.println("arrayList:" + arrayList.size() + "---"
+ (endTime - startTime));
long startTime1 = System.currentTimeMillis();
List<String> linkedList = new LinkedList<String>();
for (int i = 0; i < count; i++) {
linkedList.add("b" + i);
}
//List<String> arrayLinkedList = new ArrayList<String>(linkedList);
long endTime1 = System.currentTimeMillis();
System.out.println("arrayLinkedList:" + linkedList.size() + "---"
+ (endTime1 - startTime1));
}
I run result is this:
arrayList :100000 --- 305
arrayLinkedList :100000 --- 240
arrayList :1000000 --- 1009
arrayLinkedList :1000000 --- 817
arrayList :10000000 --- 14226
arrayLinkedList :10000000 --- 20284
But linkedlist does seem slower point , the following is the List
arrayList :10000000 --- 12890
arrayLinkedList :10000000 --- 19452
I think there is a problem lz mistake slightly , ArrayList extended time is not an extension of time for each half of the current capacity expansion , more backward, the greater the expansion , so the more backward , ArrayList extended operations less and less frequent , and certainly no N cycles times slightly.
public void ensureCapacity(int minCapacity) {
modCount++;
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) {
Object oldData[] = elementData;
int newCapacity = (oldCapacity * 3)/2 + 1;
if (newCapacity < minCapacity)
newCapacity = minCapacity;
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
}
------ For reference only ----------------------------------- ----
List
------ For reference only ---- -----------------------------------
just want to eventually get the ArrayList. . Verified once opened was quick to open up more memory than many . . . ArrayList and arrays are not the same length can not change it ? Element must be added the overall re- opened memory?
------ For reference only -------------------------------------- -
List
long startTime = System.currentTimeMillis ();
for (int i = 0; i <100000; i + +) {
arrayList.add (0, "a" + i);
}
long endTime = System.currentTimeMillis ();
System.out.println ("ArrayList:" + (endTime - startTime));
List
long startTime1 = System.currentTimeMillis ();
for (int i = 0; i <100000; i + +) {
linkedList.add (0, "a" + i);
}
long endTime1 = System.currentTimeMillis ();
System.out.println ("LinkedList:" + (endTime1 - startTime1));
------ For reference only ---------------------------------- -----
The second output is :
arrayLinkedList :100000 --- 34
I know it will be faster a little bit . . Only . I want to know why one open memory, than multiple slower to come to open up ?
------ For reference only -------------------------------------- -
------ For reference only ---------------------------------------
I found a second time should be replaced b, to avoid the impact of String pool
This Is and computer configuration ? Impossible , right ?
------ For reference only -------------------------------------- -
right. . There is also a traversal time spent . . But this is too much time spent on the point of it . More than 100,000 times the memory still open waste time ?
------ For reference only -------------------------------------- -
above the three test results are the result of a sentence : List
------ For reference only ---------------------------------------
halo, how my computer is slow ah. . .
------ For reference only -------------------------------------- -
List
is List
Your computer is a little slow , oh. . . . Haha. .
The final step is the original ArrayList once extended the length of the current general ah ? This really do not know . That if the length is very long words. It is not a waste of memory ?
If that happens , we should linkedList to do ah ? To add and delete a little bit faster . . But very few cases exist only say do not read it ? Most cases are to be read . . . . . Tasteless . .
------ For reference only -------------------------------------- -
List
is List
Your computer is a little slow , oh. . . . Haha. .
Finally is the original ArrayList once extended the length of the current general ah ? This really do not know . That if the length is very long words. It is not a waste of memory ?
If that happens , we should linkedList to do ah ? To add and delete a little bit faster . . But very few cases exist only say do not read it ? Most cases are to be read . . . . . Tasteless . .
ah , delete the phrase that you say the phrase . .
About If the length is too long , it will not be a waste of memory , the problem is not it. From the order of magnitude considered if 10,000 are full on , added 20,000 question is not great . Practical considerations may also be used as a bar, generally do not consider the size of it.
not only keep not read , ArrayList advantage is faster random access , traversing what if it should not make any difference . Which is commonly used in the operation , basically only use get (index) method , when a bit different.
没有评论:
发表评论