понеділок, 26 січня 2015 р.

String comparison in java

Comparison is the second common operation with strings in java. Let's find out which of this four functions are faster than other:

1. String.intern()
2. String.equals()
3. String.equalsIgnoreCase()
4. String.compareTo()

I decide to use in my benchmark array of 1000 random strings and compare elements from first path (0..499) with elements from second path (500..999):

  @Setup
    public void prepare() {
        testStringsPool = new String[1000];
        for (int i = 0; i < testStringsPool.length; i++) {
            int customLength = rnd.nextInt();
            if (customLength < 0) {
                customLength *= -1;
            }
            testStringsPool[i] = randomString(customLength % 20 + 10);
        }
    }

Here are my benchmark functions without annotations:

    public void intern_() {
        for (int i = 0; i < testStringsPool.length / 2; i++) {
            if (testStringsPool[i].intern() ==
                testStringsPool[testStringsPool.length - i – 1].intern());
        }
    }

    public void equals_() {
        for (int i = 0; i < testStringsPool.length / 2; i++) {
            if (testStringsPool[i].equals(
                testStringsPool[testStringsPool.length - i – 1]));
        }
    }

    public void compareTo_() {
        for (int i = 0; i < testStringsPool.length / 2; i++) {
            if (testStringsPool[i].compareTo(
                   testStringsPool[testStringsPool.length - i - 1]) == 0);
        }
    }

    public void equalsIgnoreCase_() {
        for (int i = 0; i < testStringsPool.length / 2; i++) {
            If(testStringsPool[i].equalsIgnoreCase(
                 testStringsPool[testStringsPool.length - i - 1]));
        }
    }

When we run those benchmark tests, we get something similar like this:

# Run complete. Total time: 00:00:25

Benchmark                                    Mode  Samples    Score   Error  Units
t.StringOpts.compareTo_                avgt        1          0.270 ±   NaN  us/op
t.StringOpts.equalsIgnoreCase_    avgt        1          1.323 ±   NaN  us/op
t.StringOpts.equals_                       avgt        1          0.348 ±   NaN  us/op
t.StringOpts.intern_                        avgt        1       148.612 ±   NaN  us/op


The winner is String.compareTo function

Conclusion for string comparison:

1. compareTo – is the fastest because it operates with parameter of String class without additional checking for type safety
2. equals – a bit slower by checking input parameter for the same type (String)
3. equalsIgnoreCase – more slower because all the characters are converted to uppercase in both strings
4. intern – the slowest. But when you need to work with many identical strings it can help you to reduce memory usage.

Немає коментарів:

Дописати коментар