Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which one is more efficient : List<int> or int[]

Tags:

c#

list

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 :)

like image 327
Tarik Avatar asked Jul 23 '09 00:07

Tarik


People also ask

What is more efficient List or array?

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.

Is List slower than array?

Short answer: In . NET List<T> and Array<T> have the same speed/performance because in . NET List is wrapper around Array .

Is a List more efficient than an array C#?

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.

Which List is faster in C#?

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>.


1 Answers

(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.

like image 68
Sam Harwell Avatar answered Sep 23 '22 02:09

Sam Harwell