Is there an alternative to RemoveAt?

8

I'm using Entity Framework in an Asp.Net MVC project where I have a simple list of strings and need to remove only the first item.

I used the following command:

minhaLista.RemoveAt(0);

Well, but using a Visual Studio performance tool, I could see that in a very large list the process consumes a lot of processing.

Is there an alternative that consumes less?

    
asked by anonymous 10.02.2016 / 21:02

2 answers

5

You have to analyze what you can lose. The search or index access in the structure shown by lbotinelly / Ono Sendai (whatever name he is using now: P) is bad (not that his response is bad). If that's not important, go to it.

If access is important, you have to find another structure. You may have to completely rethink what you are using. The mistake may be somewhere else. Without context it's hard to say.

One thing you can do if you can change the structure is to use an array . Then you can use ArraySegment . You can "create" a segment where the first element is not present, you do not erase the data, but you get a structure where that element is no longer accessed and the indexes are obviously repositioned. So if you take a segment starting at element 1, this element will change to 0 in the new variable. Access continues easy and fast as it is in Array or List , does not copy anything and gets what you want.

Other solutions may be possible if certain conditions can be met.

var novoArray = new ArraySegment<Cliente>(meuArray, 1, meuArray.Length - 1);
    
10.02.2016 / 21:50
5

An instance of List<> internally uses an array to keep references to collection members. This needs to be manipulated / resized when you insert or remove an item.

For insertion / removal operations, a LinkedList<> is about 100 times faster than a List<> , because its internal mechanics only references items with the nearest members.

When an item is removed, references of only 2 members (if the removed member is between two) are updated.

Source of the estimate .

Class documentation LinkedList<> on MSDN.     

10.02.2016 / 21:14