[英]why for loop is taking too much time in given java code
如何減少 for 循環所花費的時間,如果可能,使用 replaceAll(,) 方法刪除 for 循環:
String extractText(String s) throws IOException
{
String html = fj.toHtmlString(s); //extracted html source code from wikipedia
String filtered_text="";
System.out.println("extracted \n\n");
String []html_text = html.split("\n");
long start = System.currentTimeMillis();
for(String h:html_text)
{ //System.out.println("ky4"+h);
if(Pattern.compile("</strong>", Pattern.CASE_INSENSITIVE + Pattern.LITERAL).matcher(h).find())
{
}
else if(Pattern.compile("<strong", Pattern.CASE_INSENSITIVE + Pattern.LITERAL).matcher(h).find())
{
}
else
{
filtered_text += h;
filtered_text += "\n";
}
}
long end = System.currentTimeMillis();
System.out.println("loop end in "+(end-start)/1000+" seconds"+" or "+(end-start)+" miliseconds");//System.out.println(++i2+" th loop end in "+(end-start)/1000+" seconds");
return filtered_text;
}
讓我們來看看循環:
for(String h:html_text)
{ //System.out.println("ky4"+h);
if(Pattern.compile("</strong>", Pattern.CASE_INSENSITIVE + Pattern.LITERAL).matcher(h).find())
{
}
else if(Pattern.compile("<strong", Pattern.CASE_INSENSITIVE + Pattern.LITERAL).matcher(h).find())
{
}
else
{
filtered_text += h;
filtered_text += "\n";
}
}
每次循環至少要編譯一個正則表達式,這非常耗時。 改用變量:
Pattern endingTag = Pattern.compile("</strong>", Pattern.CASE_INSENSITIVE + Pattern.LITERAL);
Pattern startTag = Pattern.compile("<strong", Pattern.CASE_INSENSITIVE + Pattern.LITERAL)
for(String h:html_text)
{ //System.out.println("ky4"+h);
if(endingTag.matcher(h).find())
{}
else if(startTag.matcher(h).find())
{}
else
{
filtered_text += h;
filtered_text += "\n";
}
}
這將為您節省大量時間。 另請注意,在測量性能時,請始終在發布模式下運行。
您可以嘗試在循環之前而不是在循環中編譯您的模式:
String extractText(String s) throws IOException
{
String html = fj.toHtmlString(s); //extracted html source code from wikipedia
String filtered_text="";
System.out.println("extracted \n\n");
String []html_text = html.split("\n");
long start = System.currentTimeMillis();
Pattern end = Pattern.compile("</strong>", Pattern.CASE_INSENSITIVE + Pattern.LITERAL)
Pattern start = Pattern.compile("<strong", Pattern.CASE_INSENSITIVE + Pattern.LITERAL)
for(String h:html_text)
我的問題通過以下代碼解決:
String extractText(String s) throws IOException
{
String html = fj.toHtmlString(s);
String filtered_text="";
System.out.println("extracted \n\n");
html=html.replaceAll("(?i)</strong>", "");
html=html.replaceAll("(?i)<strong[^>]*>", "");
filtered_text = html;
long end = System.currentTimeMillis();
System.out.println("loop end in "+(end-start)/1000+" seconds"+" or "+(end-start)+" miliseconds");//System.out.println(++i2+" th loop end in "+(end-start)/1000+" seconds");
return filtered_text;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.