1

I am currently trying to improve the speed of my application by playing with my way of getting the information.

I read an html page from which I get URL and other information. For this I mainly use String.contains() and String.split(). But I was wondering what is the most efficient way to do this. I looked a bit and tried some of those but the results are quite similar for me :/

Here is a bit of my code (some part are just here for testing) :

    Pattern p = Pattern.compile("\" title=\"Read ");
    //Pattern p2 = Pattern.compile("Online\">");
    //Pattern p3 = Pattern.compile("</a></th>");
    Pattern p4 = Pattern.compile("Online\">(.*)</a></th>");

    while ((inputLine = in.readLine()) != null)
    {
        if(inputLine.contains("<table id=\"updates\">"))
        {
            tmp = inputLine.split("<tr><th><a href=\"");
            for(String s : tmp)
            {
                if(s.contains("\" title=\"Read "))
                {
                    //url = s.split("\" title=\"Read ")[0].replace(" ", "%20");
                    //name = s.split("Online\">")[1].split("</a></th>")[0];

                    url = p.split(s)[0].replace(" ", "%20");
                    //name = p3.split(p2.split(s)[1])[0];
                    Matcher matcher = p4.matcher(s);
                    while(matcher.find())
                        name = matcher.group(1); 
                    array.add(new Object(name, url));

                }
            }
            break;
        }
    }

As you can see I tried here Pattern, Matcher, split or pattern.split() but I also know that there are replaceAll or replaceFirst.

In this case what is for you the best way to do this ?

Thanks a lot.

PS: I read here : http://chrononsystems.com/blog/hidden-evils-of-javas-stringsplit-and-stringr that Pattern.split was better than split() but I couldn't find a bigger benchmark.

----- UPDATE ----

                Pattern p1 = Pattern.compile("\" title=\"Read ");
                Pattern p2 = Pattern.compile("Online\">(.*?)</a></th>");
                Matcher matcher = p2.matcher("");

                while( (inputLine = in.readLine()) != null)
                {
                    if( (tmp = inputLine.split("<tr><th><a href=\"")).length > 1 )
                    {
                        for(String s : tmp)
                        {
                            if(s.contains("\" title=\"Read "))
                            {

                                url = p1.split(s)[0].replace(" ", "%20");
                                if(matcher.reset(s).find())
                                    name = matcher.group(1); 
                                arrays.add(new Object(name, url));
                            }
                        }
                        break;
                    }
                }
Clad Clad
  • 2,217
  • 1
  • 15
  • 30

1 Answers1

2

Any string function that uses regular expressions (which are matches(s), replaceAll(s,s), replaceFirst(s,s), split(s), and split(s,i)) compiles the regular expression and creates a Matcher object every time, which is very inefficient when used in a loop.

If you need to speed thigs up, the first step is to stop using the String functions, and instead use Pattern and Matcher directly. Here's an answer where I demonstrate this.

And ideally, you should create only a single Matcher object, as I describe in this answer.

For more regex information please check out the FAQ

Community
  • 1
  • 1
aliteralmind
  • 18,274
  • 16
  • 66
  • 102
  • That's what I read on internet that they all use Pattern. But I didn't know for `split` because I used it with `Pattern.split()` so I thought no `Matcher` was included. Anyways I will try to apply your method as to use only one `Matcher` and reset it to apply it again. But I am still wondering is it faster to make a `regex` to find with backlink what is between `whatIWant` or to use two split like I did ? Thanks a lot – Clad Clad Apr 11 '14 at 06:05
  • Last question : is this regex badly done ? `"Online\">(.*)"` – Clad Clad Apr 11 '14 at 07:49
  • `Pattern.split(cs)` requires no matcher, and is more efficient than `String.split(s)`, simply because the `Pattern` object already exists. If your goal is to search `whatIWant` and match `whatIWant`, then you can use `+])>.*?\1>`. I would change your regex to `Online\">(.*?)`, which is [reluctant](http://stackoverflow.com/a/10764399), instead of greedy. Reluctant goes up to the *next* match in the search string, greedy goes to *last*. – aliteralmind Apr 11 '14 at 13:40
  • Thanks for your help :) just in your second post `matcher.reset(s).matches(...)` the last matches doesn't exit. :) – Clad Clad Apr 11 '14 at 20:21
  • I don't know what "doesn't exit" means. It just returns a boolean. – aliteralmind Apr 11 '14 at 20:25
  • I mean `matches(String)` doesn't exist in my Eclipse ^^ I updated my code, do you see something wrong ? :) – Clad Clad Apr 11 '14 at 20:34
  • Well, it's a pretty standard function. Did you import `java.util.regex.Matcher` and `Pattern`? – aliteralmind Apr 11 '14 at 21:21