微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 更改具有“return”和“yield return”的方法

我知道在同一方法中使用return和yield return是不可能的.

这是我想要优化的代码

public IEnumerable<TItem> GetItems(int data)
{
    if (this.isSingleSet)
    {
        return this.singleSet; // is IEnumerable per-se
    }
    else
    {
        int index = this.GetSet(data);
        foreach(TKey key in this.keySets[index])
        {
            yield return this.items[key];
        }
    }
}

重要提示:我知道此代码无法编译.这是我必须优化的代码.

我知道有两种方法可以使这种方法起作用:

>转换收益率返回部分:

...
else
{
    int index = this.GetSet(data);
    return this.keySets[index].Select(key => this.items[key]);
}

>转换返回部分:

if (this.isSingleSet)
{
    foreach(TItem item in this.singleSet)
    {
        yield return item;
    }
}
else ...

但两者之间存在很大的速度差异.仅使用return语句(换句话说使用Select())要慢得多(比如慢6倍)才能产生返回转换.

有没有其他方法让你想到如何编写这个方法?您是否有任何其他建议信息对性能差异有价值?

附加信息

我正在围绕for循环使用秒表来测量两种方法的速度.

Stopwatch s = new Stopwatch();
s.Start();
for(int i = 0; i < 1000000; i++)
{
    GetItems(GetRandomData()).ToList();
}
s.Stop();
Console.WriteLine(s.ElapsedMilliseconds);

每个循环都在不同的进程中运行,因此垃圾收集或其他任何东西都可能没有性能影响.

>然后我用一个方法版本运行程序
>关闭
>重写方法并再次运行.

这几次看到可靠的性能差异……

解决方法

使用两个功能.由客户端调用的外部函数执行您不想延迟的所有非惰性位(如参数验证).私人工作者做懒惰的事情:
public IEnumerable<TItem> GetItems(int data) {
  if (this.isSingleSet) {
    return this.singleSet; // is IEnumerable per-se
  } else {
    return DoGetItems(data);
  }
}

private IEnumerable<TItem> DoGetItems(int data) {
  int index = this.GetSet(data);
  foreach(TKey key in this.keySets[index]) {
    yield return this.items[key];
  }
}

原文地址:https://www.jb51.cc/csharp/98693.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐