Is there an alternative to Removeat()?

Asked

Viewed 121 times

11

I am 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 was able to notice that in a very large list the process consumes a lot of processing.

Is there any alternative that consumes less?

2 answers

7

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

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

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

Source of the estimate.

Class documentation LinkedList<> at MSDN.

6


You have to analyze what you can lose. The search or access by index in the structure shown by Ono Sendai is bad (not that his answer is bad, on the contrary), if this is not important, go to it.

If access is important, you have to find another structure. You may have to totally rethink what you’re using, the error may be somewhere else. No context is hard to say.

One thing you can do if you can change the structure is to use a array. Then you can use the ArraySegment. You can "create" a segment where the first element is not present, you don’t 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 be 0 in the new variable. Access remains easy and fast as in Array or List, copy nothing and get what you want.

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

var novoArray = new ArraySegment<Cliente>(meuArray, 1, meuArray.Length - 1);

I put in the Github for future reference.

Now you have the Span which is almost certain to be the best solution in every sense.

  • bigown thank you so much for your reply. But why exactly the answer of lbotinelly would be terrible?

  • 2

    I didn’t say his answer would be bad, she’s great, so much I said to use, as long as it can live with access with terrible speed. The linked list has access complexity O(N), as for a list or array has complexity O(1). That is, the direct access of this, even in the worst case, is almost instantaneous, just a simple operation. The list linked in the worst case has to go through the entire list to find something. This is valid for access by index or value. I do not know what is best for your case, because I do not know your case. It is possible that it is not even mine

  • Oops, now it’s much clearer. Thank you very much.

  • I didn’t know the O notation, but thanks to your comment I did a little research here and now I’m a little more on the inside (with the pinky toe at the front door actually), but I’d like to know where you got the information that the List has complexity O(n) and Array has O(1). That would be because the Array is one-way and the Multidirectional List?

  • 2

    It is a known information. A common list behaves exactly like a array, so they’re the same. Besides, it’s usually in the documentation. Knowing Big O is key to choosing the right data structure or algorithm to use. A simplified generic table (may not be valid for the specific implementation) http://bigocheatsheet.com/ Best answer on the website: http://answall.com/a/56868/101 Note that there are tricks that can make appointments change. A list may have O(N) or O(1) insertion depending on the trick and the state it is in.

  • 1

    @Jedaiasrodrigues The point of the bigown is perfect when it comes to performance. If your list exists to express a FIFO (First In, First Out) queue then a Linkedlist is the best option. But if you’re going to iterate on the list in a non-series way Linkedlist is not the best option.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.