Link List VS Dynamic Array
2013-01-03 03:07
127 查看
The size of a linked list node is two machine words for a single linked list and three for a double linked list (object reference, next reference, prev reference - roughly.. I have not yet looked at the exact .NET implementation of linked list). Plus whatever
overhead the list itself needs.
The size of a dynamic array element is only the size of the object (one machine word for reference types). Plus whatever overhead the list itself needs.
Both list and array will allocate memory in chunks of X number of objects so that they don't have to allocate memory on every insertion.
Memory isn't usually the primary consideration for which object is best for your needs. It is the cost (in time) of the various operations that will determine which you use. Here are some general costs, I'll throw Dictionary in these results as well.
Insertion to the end of either array or list is about the same cost.
Insertion to an arbitrary position is much faster on a linked list.
Insertion is relatively slow on a dictionary.
Deletion at the end of either array or list is about the same cost.
Deletion at an arbitrary position is much faster on a linked list.
Deletion is relatively slow on a dictionary (if it's a balanced tree, not sure how .net implements).
Positional access is fast on an array.
Positional access is very slow on a linked list (you'd have to enumerate N times to find node at N position).
Positional access is impossible on a dictionary as element order can change.
Searching is the same for both array and linked list (slow - every element has to be looked at on failure).
Searching is relatively fast on a dictionary.
Enumeration is about the same for both linked list and array.
Enumeration on a dictionary can be a little slower as the enumerator needs a bit more state data. Keep in mind that enumeration order can change from one enumeration to the next. Not sure if .net uses a stateful enumerator or simply allocates (and copies) an
array when asked to enumerate.
Basically you can ask these questions:
Do I need to search more often then insert/delete? Use a dictionary.
Is element order important? Do not use a dictionary.
Do I need positional access? Use an array.
Do I need to insert/delete at arbitrary points? Use a linked list.
overhead the list itself needs.
The size of a dynamic array element is only the size of the object (one machine word for reference types). Plus whatever overhead the list itself needs.
Both list and array will allocate memory in chunks of X number of objects so that they don't have to allocate memory on every insertion.
Memory isn't usually the primary consideration for which object is best for your needs. It is the cost (in time) of the various operations that will determine which you use. Here are some general costs, I'll throw Dictionary in these results as well.
Insertion to the end of either array or list is about the same cost.
Insertion to an arbitrary position is much faster on a linked list.
Insertion is relatively slow on a dictionary.
Deletion at the end of either array or list is about the same cost.
Deletion at an arbitrary position is much faster on a linked list.
Deletion is relatively slow on a dictionary (if it's a balanced tree, not sure how .net implements).
Positional access is fast on an array.
Positional access is very slow on a linked list (you'd have to enumerate N times to find node at N position).
Positional access is impossible on a dictionary as element order can change.
Searching is the same for both array and linked list (slow - every element has to be looked at on failure).
Searching is relatively fast on a dictionary.
Enumeration is about the same for both linked list and array.
Enumeration on a dictionary can be a little slower as the enumerator needs a bit more state data. Keep in mind that enumeration order can change from one enumeration to the next. Not sure if .net uses a stateful enumerator or simply allocates (and copies) an
array when asked to enumerate.
Basically you can ask these questions:
Do I need to search more often then insert/delete? Use a dictionary.
Is element order important? Do not use a dictionary.
Do I need positional access? Use an array.
Do I need to insert/delete at arbitrary points? Use a linked list.
相关文章推荐
- List vs Array:谁适合做java中泛型对象的容器?
- Dynamic-Link Libraries on Windows vs. Linux
- LeetCode :: Convert Sorted Array (link list) to Binary Search Tree [tree]
- LeetCode :: Convert Sorted Array (link list) to Binary Search Tree [tree]
- Linked List vs Array
- Array vs LinkedList
- Array vs Linked List
- C# List<>与Array性能比较
- java 集合类Array、List、Map区别和联系
- Java中array、Set、List和Map的比较总结
- .net集合类的研究--Array,ArrayList,List<T>
- FastJson中jsonArray转换成list集合
- VS2012中使用Link.exe手动链接obj
- new JSONArray(List<Map>).tostring()问题
- Hibernate的集合映射(Set、List、Array、Map、Bag)
- [2014-03-20 14:00:06] [EP000000] [执行数据库查询时发生错误]ORA-04031: 无法分配 8196 字节的共享内存 ("large pool","unknown object","hash-join subh","QERHJ list array")
- Java工具类List中的toArray方法及java.lang.ArrayStoreException详解
- Java工具类List中的toArray方法及java.lang.ArrayStoreException详解
- VS 2005中处理datagrid,绑定到List,处理列头