C#是清除列表< T>值类型仍然是一个O(n)操作? [英] C# Is clearing a List<T> with value types still an O(n) operation?

查看:94
本文介绍了C#是清除列表< T>值类型仍然是一个O(n)操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据微软文档,要求清除( )一个列表是一个O(n)操作。我猜这是因为如果入榜持有引用,那就需要为null他们。我在想,如果清除()仍然是一个O(n)操作,如果列表中有值类型,因为能力没有改变。难道不应该有足够的重置索引指针和计数?

According to the Microsoft documentation, calling Clear() on a List is an O(n) operation. I'm guessing this is because if the list were to hold references, it would need to null them. I was wondering if Clear() is still an O(n) operation if the list has value types, since the capacity is not changed. Shouldn't it be enough to reset the index pointer and count?

我问这是因为我们使用的获得在很短的时间跨度清除数十万次列出当前应用程序,并想知道是否有可能是一个不同的实现,使得它更快。

I'm asking this because in a current application we're using lists that get cleared hundreds of thousands of times in a very short time span, and wanted to know if there could be a different implementation that makes it faster.

推荐答案

在List.Clear法源$ C ​​$ C检查:

Inspecting in List.Clear method source code:

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

Array.Clear是一个外部的方法
了解 Array.Clear和MSDN声明是:

设定数组中的每个元素类型的默认值范围内的元素。

Sets a range of elements in an array to the default value of each element type.

所以它仍然是一个O(n)操作,即使T是值类型。

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

这篇关于C#是清除列表< T>值类型仍然是一个O(n)操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆