简体   繁体   English

为什么 for 循环在给定的 Java 代码中花费太多时间

[英]why for loop is taking too much time in given java code

how to reduce time taken by for loop, remove the for loop by using replaceAll(,) method if possible :如何减少 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;
}

Let's take a look a the loop:让我们来看看循环:

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";
    }
}

You compile at least one regex each time you loop, which is very time consuming.每次循环至少要编译一个正则表达式,这非常耗时。 Use variables instead :改用变量:

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";
    }
}

This will save you a lot of time.这将为您节省大量时间。 Also, note that when you measure performence, always run in Release mode.另请注意,在测量性能时,请始终在发布模式下运行。

You can try to compile your patterns before the loop and not in it as:您可以尝试在循环之前而不是在循环中编译您的模式:

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)

my problem is resoled by following code :我的问题通过以下代码解决:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM