Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java String parsing, What is faster? Regex or String methods?

Tags:

java

regex

I'm facing a dilemma. I'm parsing a string and can either do

s.matches(regex)

or I can do

s.startsWith(..) && s.endsWith(..)

As you already realize, its not a complicated regexp, and both situations will work. The idea is, the string may be very long (hundreds of chars), so I wish to maximize efficiency. What works how and better suits the issue?

like image 867
buddy123 Avatar asked Nov 06 '13 08:11

buddy123


1 Answers

Here's a really rather crude benchmark to give you an idea. Adapt it to your use cases to give you more relevant results. startsWith and endsWith are much faster. Results after 1000000 runs:

uncompiled pattern 1091ms

compiled pattern 745ms

startsWith/endsWith 24ms

public class TestRegex {

String regex = "^start.*end$";
Pattern p = Pattern.compile(regex);
String start = "start";
String end = "end";
String search = start + "fewbjlhfgljghfadsjhfdsaglfdhjgahfgfjkhgfdkhjsagafdskghjafdkhjgfadskhjgfdsakhjgfdaskhjgafdskjhgafdsjhkgfads" +end;
int runs = 1000000;


@Test
public final void test() {
    //init run
    for (int i=0;i<runs;i++)
        search.matches(regex);
    for (int i=0;i<runs;i++)
        p.matcher(search).matches();
    for (int i=0;i<runs;i++){
        search.startsWith(start);
        search.endsWith(end);
    }

    //timed run;
    Stopwatch s = Stopwatch.createStarted();
    for (int i=0;i<runs;i++)
        search.matches(regex);
    System.out.println(s.elapsed(TimeUnit.MILLISECONDS));
    s.reset();      s.start();
    for (int i=0;i<runs;i++)
        p.matcher(search).matches();
    System.out.println(s.elapsed(TimeUnit.MILLISECONDS));
    s.reset();      s.start();
    for (int i=0;i<runs;i++){
        search.startsWith(start);
        search.endsWith(end);
    }
    System.out.println(s.elapsed(TimeUnit.MILLISECONDS));

}

}
like image 85
tom Avatar answered Nov 15 '22 00:11

tom