- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我刚刚查看了 Skip
的源代码/Take
.NET Framework 的扩展方法(在 IEnumerable<T>
类型上),发现内部实现正在使用 GetEnumerator
方法:
// .NET framework
public static IEnumerable<TSource> Skip<TSource>(this IEnumerable<TSource> source, int count)
{
if (source == null) throw Error.ArgumentNull("source");
return SkipIterator<TSource>(source, count);
}
static IEnumerable<TSource> SkipIterator<TSource>(IEnumerable<TSource> source, int count)
{
using (IEnumerator<TSource> e = source.GetEnumerator())
{
while (count > 0 && e.MoveNext()) count--;
if (count <= 0)
{
while (e.MoveNext()) yield return e.Current;
}
}
}
假设我有一个 IEnumerable<T>
有 1000 个元素(基础类型是 List<T>
)。如果我正在执行 list.Skip(990).Take(10) 会发生什么?它会在取最后十个元素之前遍历 990 个第一个元素吗? (这就是我的理解)。如果是,那么我不明白为什么 Microsoft 没有实现 Skip
像这样的方法:
// Not tested... just to show the idea
public static IEnumerable<T> Skip<T>(this IEnumerable<T> source, int count)
{
if (source is IList<T>)
{
IList<T> list = (IList<T>)source;
for (int i = count; i < list.Count; i++)
{
yield return list[i];
}
}
else if (source is IList)
{
IList list = (IList)source;
for (int i = count; i < list.Count; i++)
{
yield return (T)list[i];
}
}
else
{
// .NET framework
using (IEnumerator<T> e = source.GetEnumerator())
{
while (count > 0 && e.MoveNext()) count--;
if (count <= 0)
{
while (e.MoveNext()) yield return e.Current;
}
}
}
}
事实上,他们这样做是为了 Count
方法例如...
// .NET Framework...
public static int Count<TSource>(this IEnumerable<TSource> source)
{
if (source == null) throw Error.ArgumentNull("source");
ICollection<TSource> collectionoft = source as ICollection<TSource>;
if (collectionoft != null) return collectionoft.Count;
ICollection collection = source as ICollection;
if (collection != null) return collection.Count;
int count = 0;
using (IEnumerator<TSource> e = source.GetEnumerator())
{
checked
{
while (e.MoveNext()) count++;
}
}
return count;
}
那是什么原因呢?
最佳答案
在 Jon Skeet 的优秀教程中重新实现 Linq ,他(简短地)讨论了这个问题:
Although most of these operations can't be sensibly optimized, itwould make sense to optimize Skip when the source implements IList.We can skip the skipping, so to speak, and go straight to theappropriate index. This wouldn't spot the case where the source wasmodified between iterations, which may be one reason it's notimplemented in the framework as far as I'm aware.
这似乎是推迟优化的合理理由,但我同意对于特定情况,如果您可以保证您的源不能/不会被修改,那么进行优化可能是值得的。
关于c# - Skip 的性能(以及类似的功能,如 Take),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24012667/
我正在做一个项目,我需要一个 skip-gram 模型的预训练向量。我听说还有一个名为 skip-n-gram 模型的变体可以提供更好的结果。 我想知道自己训练模型需要什么?因为我只需要它们来为我的模
我正在尝试实现 EF 提供的分页功能。我以为我只需要将简单的 Skip() 和 Take() 添加到我的查询中,但后来我收到了这条消息: Error 4 'System.Linq.IOrderedQu
到目前为止,我有一个语法一直在使用标准的 boost::spirit::ascii::space/boost::spirit::ascii::space_type 船长。 我有一些使用船长的规则和一些
我正在处理动态数据。 创建动态模型并在global.asax中注册后,如 DefaultModel.RegisterContext(typeof(masterEntities1),new Contex
在 Visual Studio 2013 Update 1 中使用 Entity Framework 6.0.2 和 .NET 4.5.1 DbContext连接到 SQL Server: 我有一个很
我正在创建一个支持动态数据的 ASP.NET 网站。当我从头开始(从 VS 中的模板)创建动态网站时,一切正常。但是,当我尝试添加动态实体 (.edmx) 文件并运行应用程序时,出现以下错误: “Sk
我们可以跳过一个可选参数并为跳过的参数赋值吗? 例如我有一个函数: public function Dialog(message:String,title:String="Note",dialogsi
我创建了一个函数来从我的 firestore 集合中提取一个随机文档,然后返回一个 FIRQuery。得到它后,我创建了一个模型文件来解析我的 firestore 文档中的任何 FIRQuery 数据
我对编程比较陌生,我想尝试看看如何在 Visual Studio 2019 中制作基本的 Windows 桌面应用程序。由于我为按钮添加了代码,所以 C2360 和 C2361 错误不断弹出,但我没有
我正在使用 Data Skipping Indexes clickhouse 中的功能,我对它的用法感到困惑。如果我在创建表时添加数据跳过索引,如下所示: CREATE TABLE MyTable (
我不确定Maven指令-Dmaven.test.skip.exec和-Dmaven.test.skip=true -DskipTests之间的区别是什么。两者似乎都抑制了测试周期。 最佳答案 “mav
假设我有一些神奇的分页黑盒类,它使用 pageIndex 和 pageSize 检索数据,如下所示: public class PaginatedList { // ... // Ch
我计划使用 Bowerjs 来管理前端依赖项,并使用 gulp.js 来连接、缩小和编译前端代码。因此,我想跳过 Rails Assets 管道,包括 sprocket。我运行以下命令来跳过 Asse
我计划使用 Bowerjs 来管理前端依赖项,并使用 gulp.js 来连接、缩小和编译前端代码。因此,我想跳过 Rails Assets 管道,包括 sprocket。我运行以下命令来跳过 Asse
我正在索引页上的 MVC 中进行分页......在这一行我得到了错误 return View( employee.ToPagedList(Page ?? 1,3)); 这里是索引方法 public
我正在搜索关于跳过连接的科学工作。 每个人都在谈论通过网络改进梯度流,这感觉很有意义。但是我很想理解和阅读归根结底的数学和代码意味着什么以及如何在 tensorflow 中深入实现它。 resnet
添加 skip规则不符合我的期望。这是由逗号和空格分隔的一对标记的语法。我制作了一个版本,其中逗号标记为 skip ,还有一个不是: grammar Commas; COMMA: '
将Jupyter用于幻灯片时,是否有默认方法将幻灯片类型设置为跳过新单元格的? 最佳答案 我针对您所要求的解决方法。 创建一个空的“跳过”幻灯片并键入: 退出 C 伏特 伏特 伏特 复制并粘贴许多空的
我正在尝试使用更新 svn update --username myusername https://my.svn.address 但是,我只是收到一条“已跳过”消息? 最佳答案 我猜你遇到了这种类型
Flash 的 MovieClip 时间轴的创建方式可以跳过帧以保持动画平滑度和音频同步。 我的问题是,如果帧上有 ActionScript,是否有可能跳过该帧,从而不调用脚本? 或者有脚本的帧永远不
我是一名优秀的程序员,十分优秀!