简体   繁体   English

清除数组的最有效方法是什么?

[英]What's the most efficient way to clear an array?

I was looking through some old code of mine from when I just started programming, and saw this lovely gem: 当我刚开始编程时,我正在浏览我的一些旧代码,并看到了这个可爱的宝石:

int[] Answers = new int[4];

//Some amount of code later, inside a function
Answers[0] = Answers[1] = Answers[2] = Answers[3] = 0;

Now although it works, it doesn't look very efficient because of the 4 assignment operators, so I did some researching, and found some alternatives: 现在,尽管它可以工作,但是由于有4个赋值运算符,它看起来效率不高,所以我做了一些研究,找到了一些替代方案:

Array.Clear(Answers, 0, 4);
((IList)Answers).Clear();
Parallel.For(0, 4, index => Answers[index] = 0);

There are many others, for example a friend mentioned Buffer.BlockCopy and Enumerable.Repeat . 还有很多其他的东西,例如一个朋友提到Buffer.BlockCopyEnumerable.Repeat Which of these is the most efficient, or if there's another that's more efficient, what is it? 其中哪一个是最有效的,或者如果还有另一个更有效,那是什么?

What's the most efficient way to clear an array? 清除数组的最有效方法是什么?

Efficiency depends on the context and the final goal of the code and how many items are in the array. 效率取决于代码的上下文和最终目标以及数组中有多少项。 The acquisition of memory locations might be faster than iterating over an array to clear it. 内存位置的获取可能比遍历数组以clear它更快。 Depending on the type of memory used to hold the data there have been optimized methods to Clear the list or array of data which usually is faster than doing it by hand. 根据用于保存数据的内存类型,已优化Clear数据列表或数据数组的方法,通常比手工完成要快。


With that said, though one rarely clears an array directly and simply lets the array be garbage collected (on off cycles for the most part) by creating a new array with the clean values of the array available. 随着中说,虽然一个很少直接清除阵列,并且简单地通过创建具有可用阵列的清洁值的新的数组让阵列被垃圾收集 (ON OFF周期的大部分)。

Subsequently by adhering to better programming disciplines, one rarely specifies an array size to be created and cleared to have it be re-used. 随后,通过遵循更好的编程规范,很少会指定要创建的数组大小并清除以重新使用它。

For the re-use and array size creation paradigm can introduce subtle and non subtle errors in indexing and general operations. 对于重用和数组大小创建范例,可能在索引和常规操作中引入细微和非细微的错误。 Because the safety of creating data lists (arrays) on the fly and adding into them (inserting) and letting the language handle the memory allocations/de-allocations of data is a much safer and more efficient method to handle data operations. 动态创建数据列表(数组)并将其添加(插入)并让语言处理数据的内存分配/取消分配的安全性是一种处理数据操作的更安全, 更有效的方法。


Hence making it pointless to clear any array for re-use. 因此,清除任何数组以供重用变得毫无意义。 We no longer program on 8088 based machines where memory and processing times are at a premium. 我们不再在内存和处理时间非常宝贵的基于8088的计算机上编程。 Frankly trying to optimize little code (and later maintaining that code either by the original coder or a new developer) for a majority of programming needs on a modern computer is foolish and wastes the valuable development time for a company. 坦率地尝试针对现代计算机上的大多数编程需求优化少量代码 (然后由原始编码人员或新开发人员维护该代码)是愚蠢的,并且浪费了公司宝贵的开发时间

That programmer's time is more expensive to a development organization than the savings of a few cycles of time which the user would most likely not notice anyway. 对于程序员来说,程序员的时间要比节省几个时间的循环所花费的时间要多得多,而用户很少会注意到这些时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 生成缩略图的最有效方法是什么? - What's the most efficient way to generate thumbnails? 按字母顺序排序的最有效方法是什么? - What's the most efficient way to sort this alphabetically? 复制C#多维数组元素的最有效方法是什么? - What's the most efficient way to copy elements of a C# multidimensional array? 管理大量数据(高度数据)并替换这个巨大数组的最有效方法是什么? - What's the most efficient way to manage large amounts of data (height data) and replace this huge array? 将行为注入我所有实体的最有效方法是什么? - What's the most efficient way to inject behavior into all my entities? 在XDocument中查找和设置元素值的最有效方法是什么? - What's the most efficient way to locate and set element values in an XDocument? 什么是将DataTable转换为对象的最有效方法[,]? - What's the most efficient way to convert a DataTable to an object[,]? 访问.html页面的最有效方法是什么? - What's the most efficient way to visit a .html page? 加载用户的DirectoryEntry的最有效方法是什么? - What is the most efficient way to load a user's DirectoryEntry? 每 20 秒调用一次方法的最有效方法是什么 - What's the most efficient way to call a method every 20 seconds
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM