Can someone tell me which one is more efficient between List<int>
and int[]
. Because I am working on a project and as you might know efficiency is way so important concern now.
If you added some introductory note to your post, it'd be great tho :)
An array is faster and that is because ArrayList uses a fixed amount of array. However when you add an element to the ArrayList and it overflows. It creates a new Array and copies every element from the old one to the new one. List over arrays.
Short answer: In . NET List<T> and Array<T> have the same speed/performance because in . NET List is wrapper around Array .
In general, it's better to use lists in C# because lists are far more easily sorted, searched through, and manipulated in C# than arrays.
From the link Difference between O(n) and O(log(n)) - which is better and what exactly is O(log(n))? We can know O(log n) is better than O(n), therefore List<T>. BinarySearch will be faster than List<T>.
(list should be resizable) ? List<int> : int[]
List<int>
is a wrapper for int[]
that resizes as needed. With JIT inlining, they should perform almost identically, but the JIT will have an easier time edging out the extra performance from int[]
because it's a CLI primitive with dedicated IL instructions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With