C # Clears list <T> when value types still perform O (n) operation?

According to Microsoft documentation, calling Clear () on a list is an O (n) operation. I suppose this is because if the list should contain links, it will need to reset them. I was wondering if Clear () is still an O (n) operation, if the list has value types, since the capacity does not change. Shouldn't that be enough to reset the pointer pointer and count?

I ask this because in the current application we use lists that are cleared hundreds of thousands of times in a very short period of time, and wanted to know if there could be another implementation that speeds up its execution.

+6
source share
1 answer

Check in the source code of List.Clear:

Array.Clear(_items, 0, _size); _size = 0; 

Array.Clear is the extern method and the MSDN statement about Array.Clear :

Sets the default range of elements in an array for each element type.

So this is still an O (n) operation, even if T is a value type.

+4
source

Source: https://habr.com/ru/post/985957/


All Articles