4
\$\begingroup\$

I want to get text1 and text2 by splitting time format

Text1 10:24:02 Text2

Of the two working regexes below, which is faster?

Regex 1

String regex1= "([0-9]{2}):([0-9]{2}):([0-9]{2})";

Regex 2

String regex2= "[0-9][0-9]:[0-9][0-9]:[0-9][0-9]";

This is the code I've used:

String s="Text1 10:24:02 Text2";
String[] split= s.split(regex1);//regex 1 and 2
System.out.println(split[0]);
System.out.println(split[1]);
\$\endgroup\$
4
\$\begingroup\$

Regex 2 is faster, but probably not for the reason that you expect.

You can easily answer questions like these by writing a benchmark. Here is an example:

public static long splitTime(String regex, String text) {
    long start = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        String[] split = text.split(regex);
    }
    long end = System.currentTimeMillis();
    return end - start;
}

public static void main(String[] args) {
    String regex1 = "([0-9]{2}):([0-9]{2}):([0-9]{2})";
    String regex2 = "[0-9][0-9]:[0-9][0-9]:[0-9][0-9]";
    String s="Text1 10:24:02 Text2";

    // Warm up the loops
    for (int i = 0; i < 2000; i++) {
        splitTime(regex1, s);
        splitTime(regex2, s);
    }

    long time0 = 0, time1 = 0, time2 = 0;
    for (int i = 0; i < 2000; i++) {
        time1 += splitTime(regex1, s);
        time2 += splitTime(regex2, s);
        time2 += splitTime(regex2, s);
        time1 += splitTime(regex1, s);
    }

    System.out.println("Regex 1: " + time1);
    System.out.println("Regex 2: " + time2);
}

The JIT compiler tends to do funny tricks. For fairness, I've warmed up the loop by executing both of them without timing. I've also interleaved the calls to splitTime in case the order somehow makes a difference.

I found that Regex 1 is slower than Regex 2 by about 5%.

However, Regex 1 has some capturing parentheses. If you remove them,

    String regex0 = "[0-9]{2}:[0-9]{2}:[0-9]{2}";

then you get a result that is 16% faster than Regex 2.

\$\endgroup\$
  • 1
    \$\begingroup\$ To make it even faster: [0-9]{2}(?::[0-9]{2}){2} \$\endgroup\$ – Unihedron Oct 19 '14 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.