Array.Copy VS跳过,并在C#中取 [英] Array.Copy vs Skip and Take in c#
问题描述
我在浏览这个问题,一些类似的:
I was browsing this question and some similar ones:
<一个href=\"http://stackoverflow.com/questions/943635/c-arrays-getting-a-sub-array-from-an-existing-array\">C#阵列,获取从现有阵列的子阵列
很多地方我看了这样的答案:
Many places I read answers like this:
<一个href=\"http://stackoverflow.com/questions/943635/c-arrays-getting-a-sub-array-from-an-existing-array/943644#943644\">C#阵列,获取从现有阵列的子阵列
我想知道就是为什么跳过并以不适合数组常量时间操作?
What I am wondering is why Skip and Take are not constant time operations for arrays?
在反过来,如果他们是固定的时间操作,也不会跳过和采取的方法(结尾没有调用ToArray的())具有相同的运行时间没有做一个Array.Copy的开销,同时也更多的空间效率?
In turn, if they were constant time operations, won't the Skip and Take method (without calling ToArray() at the end) have the same running time without the overhead of doing an Array.Copy, but also more space efficient?
推荐答案
您的跳过
和把工作区分code>方法做的,和消费数据,该方法返回的工作。
You have to differentiate between the work that the Skip
and Take
methods do, and the work of consuming the data that the methods return.
的跳过
和以
方法本身是O(1)操作,因为他们几乎没有在所有的工作。他们只是建立了一个枚举器,它能够从阵列返回的项目。
The Skip
and Take
methods themselves are O(1) operations, as they do practically no work at all. They just set up an enumerator that is capable of returning items from the array.
这是当您使用枚举的工作就完成了。这是一个O(n)操作,其中n是枚举器产生的项目数。由于统计员从阵列读取时,不包含数据的副本,你必须保持数据数组完好,只要您使用的是枚举。
It's when you use the enumerator that the work is done. That is an O(n) operation, where n is the number of items that the enumerator produces. As the enumerators read from the array, they don't contain a copy of the data, and you have to keep the data in the array intact as long as you are using the enumerator.
(如果你使用跳过
上的集合,是不能访问通过索引像一个数组,流汗的第一个项目是一个O(n)操作,其中n是项的数目被跳过。)
(If you use Skip
on a collection that is not accessible by index like an array, gettting the first item is an O(n) operation, where n is the number of items skipped.)
这篇关于Array.Copy VS跳过,并在C#中取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!