简体   繁体   中英

C# array or List(T) for max performance

Can anyone advice on what to choose in this situation: I have 100-500(they dynamic, means on every request their number always diffrent) elements which contains element name, type, id. Currently I using multidimensional array

public static Object[,] Item_data = new Object[500, 3];

And then I set data to array:

int found_items = 0;
        foreach (Object m in queryCollection)
        {
            Item_data[found_items, 0] = m[0];

            Item_data[found_items, 1] = m[1];

            Item_data[found_items, 2] = m[0];
            found_plans++;
        }

And I have 8 other same structure arrays which fills different data, it cost around 0.8-1.5 seconds, problem is I need to sort these arrays ASC, DESC by id, by name and by type, if i do manually using another loop to sort data it cost time, so I noticed about List(T) it has sort feature, but its much slower according to these topics:

Performance of Arrays vs. Lists

https://jacksondunstan.com/articles/3058

https://softwareengineering.stackexchange.com/questions/221892/should-i-use-a-list-or-an-array

Is it worth to use List(T) in this situation? Or can anyone recommend something else?

If you need fast access to the structure, have you tried using Dictionary<> ? You can sort it using System.Linq

https://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx

List<T> internally implements T[] , just that its flexible and internally keeps expanding when Capacity is over. Since for the all the important operations, it internally does the array operations, just that it provides the convenience for the dynamic expansion.

As per your question:

I have 100-500(they dynamic, means on every request their number always different)

Now ideally a new List<T>(100 / 200) is a better and most optimum choice, since it would dynamically expand for including more data, though please note there's no concept of Multi dimensional List, like Multi dimensional Array. Though you can have something similar to Jagged array T[][] , using something like List<List<T>> , but there's no replacement of T[,] using List<T> . Multidimensional array works well for a matrix form of data with defined lower and upper bounds.

This is related to memory optimization, when it comes to various operations, List<T> exposes Array operations (In place sorting), List<T> and T[] also exposes the Linq extension API for IEnumerable<T> , which are not as efficient as in place sorting, due to extra memory allocation and they are done on generic interface instead of specific data structure.

Now regarding various use cases:

  • If it's all about enumeration / data processing sequentially, then both T[] and List<T> are efficient, in fact they provide a binary search option, which makes search for sorted data as O(LogN) , much better than O(N) , in fact you may consider SortedList<TK,TV> , which is by default sorted, but just that its IDictionary internally, similar is the SortedDictionary<TK,TV> , for very fast element search there's no replacement of IDictionary<TK,TV> , it is O(1) .

All the above points are mere theory best way to find the right fit with a combination of memory and performance is to test your use cases with variety of data structures and you will be able to get the right fit for all use cases with minimal compromise

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM