通过使用产量或改变算法来优化代码
下面的代码可以工作,但我想使用产量或通过更改算法来优化代码。
public IEnumerable<Book> GetAuthorWithBookName(string keyword)
{
var bookAndAuthorList = new List<Book>();
List<Author> AuthorNameList = getAuthorName(keyword);
foreach (var author in AuthorNameList)
{
XmlNode booksNames = getBook(author);
XDocument XDOCbooksNames = XDocument.Parse(booksNames.OuterXml);
var bookNameList = (
from x1 in XDOCbooksNames.Descendants("Books")
select x1.Elements("book").Select(g => g.Attribute("name").Value))
.ToList();
foreach (var bookName in bookNameList)
{
bookAndAuthorList.Add(new Book()
{
authorName = author.authorName,
bookName = bookName
});
}
}
return bookAndAuthorList;
}
public class Book
{
public string authorName { get; set; }
public string bookName { get; set; }
}
The code below works but I want to optimize the code using yield or by changing algorithm.
public IEnumerable<Book> GetAuthorWithBookName(string keyword)
{
var bookAndAuthorList = new List<Book>();
List<Author> AuthorNameList = getAuthorName(keyword);
foreach (var author in AuthorNameList)
{
XmlNode booksNames = getBook(author);
XDocument XDOCbooksNames = XDocument.Parse(booksNames.OuterXml);
var bookNameList = (
from x1 in XDOCbooksNames.Descendants("Books")
select x1.Elements("book").Select(g => g.Attribute("name").Value))
.ToList();
foreach (var bookName in bookNameList)
{
bookAndAuthorList.Add(new Book()
{
authorName = author.authorName,
bookName = bookName
});
}
}
return bookAndAuthorList;
}
public class Book
{
public string authorName { get; set; }
public string bookName { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
鲁本斯和卢克的回应正确解释了产量的使用。
然而,这对我来说看起来很可疑。
您将 XML 转换为字符串,然后再次解析它,只是因为您想将其从 DOM 节点转换为 Xml.Linq 节点。如果你谈论的是优化,那么这比创建额外的列表效率低得多。
Responses by Rubens and Luke correctly explain the use of yield.
However, this looks suspicious to me.
You convert XML to string and then parse it again, only because you want to convert it from DOM Node to Xml.Linq node. If you are talking about optimization, then this is much more inefficient than creating an extra list.
为了快速获胜,您可以删除
.ToList()
调用。您所做的只是枚举项目,因此无需这样做。同样,无需创建 bookAndAutherList。最终我认为你可以将其简化为:
As a quick win, you can remove the
.ToList()
call. All you're doing is enumerate items, so there's no need to do that. Similarly, there's no need to create bookAndAutherList.Eventually I think you can strip it down to this:
不确定你会得到很多“优化”。当您经常在使用正在生成的枚举时中断部分时,最好使用yield,这样您就不会进行不必要的计算。但这里有一句话:
这是未经测试的。
我做了什么:
表达式,它已经返回一个
可数。删除了一些代码
将 getAuthorName 合并到
foreach(注意 - 如果可以的话,请确保该函数也能产生并且可枚举)
Not sure you'll get much 'optimization'. Yield is better used when you would often break part way through using the enumeration you are generating, so that you don't do unnecessary computation. But here goes:
This is untested.
What I did:
expression, it already returns an
enumerable. removed some code by
consolidating getAuthorName into the
foreach (note - make sure that function yields and ienumerable too, if you can)
试试这个:
并删除其他
return
语句。Try this:
And remove other
return
statement.