如何检测和避免无限循环?
我想确保我的 Web 应用程序性能良好并且不会出现任何性能问题。当我搜索时,我发现与性能问题相关的最常见问题之一是“无限循环”问题。
我想问:
我应该从哪里开始检查我的代码不会导致无限循环?
有文章、建议、说明、示例吗?我将不胜感激。
例如:
这段代码可能会导致无限循环吗?
public static IEnumerable<SubjectNode> ReadSubjectNodes(string filePath)
{
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (XmlReader xrdr = new XmlTextReader(fs))
while (xrdr.Read())
if (xrdr.NodeType == XmlNodeType.Element && xrdr.LocalName == "subject")
yield return new SubjectNode(xrdr.GetAttribute("id"), xrdr.GetAttribute("name"), xrdr.GetAttribute("short"));
}
提前致谢
I want to make sure that my web application is performance wise and will not make any performance problems. When I searched, I found that one of the most common problems related to the performance issue is the "infinite loops" problem.
I want to ask:
Where should I begin to check that my code never causes infinite loops?
Are there any articles, advices, instructions, examples? I will be grateful.
ex:
May this code cause an infinite loop?
public static IEnumerable<SubjectNode> ReadSubjectNodes(string filePath)
{
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (XmlReader xrdr = new XmlTextReader(fs))
while (xrdr.Read())
if (xrdr.NodeType == XmlNodeType.Element && xrdr.LocalName == "subject")
yield return new SubjectNode(xrdr.GetAttribute("id"), xrdr.GetAttribute("name"), xrdr.GetAttribute("short"));
}
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,据我所知,代码不会导致无限循环 - 它不是递归的,文件系统中的
XmlReader
最终将耗尽数据。不过,它可能会长时间运行。在某些情况下,添加健全性检查可能会有所帮助 - 这可能是计数器或时间检查。例如:Well, AFAIK that code won't cause an infinite loop - it is not recursive, and an
XmlReader
from the file-system will eventually run out of data. It might be long running, though. In some cases, adding a sanity check may help - this could be a counter, or a check against the time. For example:没有办法确定性地预先确定循环是否将运行无限次,除了那些具有编译时定义的迭代次数并且不改变其主体中的迭代计数器的循环之外。
只需编写不易出现无限循环的好代码即可:检查 while 循环内的条件并考虑它们可能导致无限循环的情况。就是这样。
There is no way to deterministically pre-determine whether a loop will run infinite times, except for the ones which have a compile-time defined number of iterations and don't alter the iteration counter in their body.
Just write good code which is not prone to infinite looping: inspect the conditions inside the while loops and think about a case in which they might cause infinite looping. That's it.
您在此处发布的代码有一个退出条件:当
XmlTextReader
到达其流的末尾时。因此,它不是无限循环。这确实是您需要做的事情来防止这些情况:确保每个循环都有一个导致其退出的条件。编写代码时比稍后检查要容易得多。
The code you have posted here has an exit condition: When the
XmlTextReader
reaches the end of its stream. Therefore, it's not an infinite loop.That's really what you need to do to prevent these: make sure every loop has a condition which will cause it to exit. It's much easier to do when writing the code than reviewing it later.
当您循环直到满足参数( while 循环)时,就会发生无限循环,但 while 子句可能永远无法满足退出循环的要求。因此它会永远持续下去。
也有可能导致递归循环,即您从自身内部(或类似的)调用函数,它将不断调用函数,但永远不会接近退出。
while (xrdr.Read())
,只要.Read()
自动调用移动到下一个元素就可以了。此时,由于您的 xrdr 不是无限源,因此它最终会到达终点并退出。Infinate loops occur when you are looping until a parameter is met ( a while loop ), but it is possible that the while clause is never satisfied to exit the loop. And hence it goes on forever.
It is also possible to cause a recursive loop, by where you call a function from within itself (or similar), where it will constantly call up a function, but never get closer to exiting.
while (xrdr.Read())
, is fine as long as.Read()
is invoking the moving to the next element automatically. At which, as your xrdr is not an infinite source, it will eventually reach the end and exit.