问题描述
对于我的工作,我必须开发一个小型Java应用程序来解析非常大的XML文件(~300k行)以选择非常具体的数据(使用 Pattern
),所以我我试图优化它一点。我想知道这2个片段之间有什么好处:
For my work I have to develop a small Java application that parses very large XML files (~300k lines) to select very specific data (using Pattern
), so I'm trying to optimize it a little. I was wondering what was better between these 2 snippets :
if(boolean_condition && matcher.find(string))
{
...
}
OR
if(boolean_condition)
{
if(matcher.find(string))
{
...
}
}
更精确:
- 这些if语句在循环内的每次迭代中执行(~20k迭代)
-
boolean_condition
是使用外部函数在每次迭代时计算的boolean
- 如果
boolean
设置为false
,我不需要测试匹配的正则表达式
- These if statements are executed on each iteration inside a loop (~20k iterations)
- The
boolean_condition
is aboolean
calculated on each iteration using an external function - If the
boolean
is set tofalse
, I don't need to test the regular expression for matches
感谢您的帮助
推荐答案
我遵循的一条黄金法则是尽可能多地避免嵌套。但是如果以使我的单一if条件过于复杂为代价,我不介意将其嵌套。
One golden rule I follow is to Avoid Nesting as much as I can. But if it is at the cost of making my single if condition too complex, I don't mind nesting it out.
除了你正在使用短路&&
运算符。因此,如果布尔值为false,它甚至不会尝试匹配!
Besides you're using the short-circuit &&
operator. So if the boolean is false, it won't even try matching!
所以,
if(boolean_condition && matcher.find(string))
{
...
}
是要走的路!
这篇关于什么是更好的 ?多个if语句,如果有多个条件,则为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!