本文介绍了是的LINQ的跳过,并采取阵列优化? [4.0版]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是从一个数组复制的范围内的常见情况。 C#支持这个操作以各种方式,例如使用Array.Copy,也受到的LINQ的跳过和径组合

It's a common situation to copy a range from an array. C# supports this operation in various ways, for instance using Array.Copy, but also by Linq's Skip and Take combination.

在开始.NET 4.0,请跳过和采取行动仍在增加相当大的开销,还是他们承认(无论是在编译时或运行时),他们的目标是一个数组?

Starting at .NET 4.0, do the Skip and Take operations still add considerable overhead, or do they recognize (either at compile time or at run time) their target is an array?

要澄清:我是指一)采取跳过最初的字节和时间二)跳过通吃的ToArray,这并不副作用遭受的组合。例如,新的byte [10000] .Skip(9999)。取(1).ToArray()可以的进行优化,这种方式会(对于大n)是明显加快。

To clarify: I am referring to a) the time taken to skip the initial bytes and b) to the combination of Skip-Take-ToArray, which does not suffer from side effects. For instance, new byte[10000].Skip(9999).Take(1).ToArray() could be optimized this way and would (for large n) be noticeably faster.

推荐答案

跳转/拍摄静态O(n)的操作,由于LINQ怎么还保持怎么连数组中的元素是可以改变的知识和对象如何有留意识到changes.Therefore,而优化到O(1)似乎微不足道,但目前尚未实现。

Skip/Take still operate in O(n) due to how LINQ has to maintain knowledge of how elements even in arrays can be changed and how the objects have to stay aware of the changes.Therefore, while the optimizations to O(1) seems trivial, it is not currently realized.

请参阅:的https://edulinq.google$c$ c.com/hg/posts/40-Optimization.html

乔恩斯基特有一个优秀的博客在几年前讨论这些多在重新实现LINQ(<一href="http://$c$cblog.jonskeet.uk/2011/01/02/reimplementing-linq-to-objects-part-23-take-skip-takewhile-skipwhile/" rel="nofollow">http://$c$cblog.jonskeet.uk/2011/01/02/reimplementing-linq-to-objects-part-23-take-skip-takewhile-skipwhile/).这是一个伟大的阅读。

Jon Skeet had an excellent blog a few years back discussing these and more in "reimplementing LINQ" (http://codeblog.jonskeet.uk/2011/01/02/reimplementing-linq-to-objects-part-23-take-skip-takewhile-skipwhile/). It's a great read.

这篇关于是的LINQ的跳过,并采取阵列优化? [4.0版]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 08:49