Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is ".concat(String)" so much faster than "+"? [duplicate]

Some code I made compares the time it takes to concatenate strings with "string" + "string":

for(int i = 0; i < 100000000L; i++)
{
    String str2 = str + str;
}

to "string".concat("string"):

for(int i = 0; i < 100000000L; i++)
{
    String str2 = str.concat(str);
}

Where str == "string".

The output I get is consistently similar to this, although the average difference is generally closer to 61 nanoseconds:

String str2 = str + str: 118.57349468 nanoseconds

String str2 = str.concat(str): 52.36809985 nanoseconds

.concat is faster than + by 66.20539483 nanoseconds

This shows that even with the loop and assignment to a new string, .concat is faster than + by a factor of more than two. When I use an even longer string (str == "this is a really really very long string that is very long"), it is faster by about a factor of three. This is especially strange, because if .concat is faster, shouldn't they make + compile to .concat?

My main question is: Why is .concat faster?

Full code, in case you want to run it and experiment with it:

public class TimeCompare
{
    public static void main(String[] args)
    {
        final long times = 100000000L;

        String str = "String";

        long start1 = System.nanoTime();

        for(int i = 0; i < times; i++)
        {
            String str2 = str + str;
        }

        long end1 = System.nanoTime();
        long time1 = end1 - start1;

        System.out.println((double)(time1) / times);
        System.out.println();

        long start2 = System.nanoTime();

        for(int i = 0; i < times; i++)
        {
            String str2 = str.concat(str);
        }

        long end2 = System.nanoTime();
        long time2 = end2 - start2;

        System.out.println((double)(time2) / times);
        System.out.println();

        System.out.println(".concat is faster than \"+\" by " + ((double)(time1 - time2) / times) + " nanoseconds");
    }
}
like image 864
The Guy with The Hat Avatar asked Mar 08 '14 17:03

The Guy with The Hat


1 Answers

Edit: Looking back at this answer, I now realize how unscientific and speculative it is. While it isn't necessarily wrong, I no longer have confidence in its correctness.


Here is the source code for concat:

public String concat(String str) {
    int otherLen = str.length();
    if (otherLen == 0) {
        return this;
    }
    int len = value.length;
    char buf[] = Arrays.copyOf(value, len + otherLen);
    str.getChars(buf, len);
    return new String(buf, true);
}

"string" + "string" compiles to new StringBuilder().append("string").append("string").toString().1append's source uses its superclass', AbstractStringBuilder, method:

public AbstractStringBuilder append(String str) {
    if (str == null) str = "null";
    int len = str.length();
    ensureCapacityInternal(count + len);
    str.getChars(0, len, value, count);
    count += len;
    return this;
}

After replacing method calls with the method's source:

/////////////////concat

int otherLen = str.length();
if (otherLen == 0) {
    return this;
}

int len = value.length;

char buf[] = ((Object)value.getClass() == (Object)Object[].class)
    ? (T[]) new Object[len + otherLen]
    : (T[]) Array.newInstance(value.getClass().getComponentType(), len + otherLen);

System.arraycopy(value, 0, buf, 0, Math.min(value.length, len + otherLen));

System.arraycopy(str.value, 0, buf, len, str.value.length);

return new String(buf, true);

///////////////append

if (str == null) str = "null";
int len = str.length();

if (value.length + len - value.length > 0)
{
    int newCapacity = value.length * 2 + 2;
    if (newCapacity - value.length + len < 0)
        newCapacity = value.length + len;
    if (newCapacity < 0) {
        if (value.length + len < 0) // overflow
            throw new OutOfMemoryError();
        newCapacity = Integer.MAX_VALUE;
    }

    value = ((Object)value.getClass() == (Object)Object[].class)
        ? (T[]) new Object[newCapacity]
        : (T[]) Array.newInstance(value.getClass().getComponentType(), newCapacity);

    System.arraycopy(value, 0, value, 0, (value.length <= newCapacity) ? value.length : newCapacity;
}

if (0 < 0) {
    throw new StringIndexOutOfBoundsException(0);
}
if (len > str.value.length) {
    throw new StringIndexOutOfBoundsException(len);
}
if (0 > len) {
    throw new StringIndexOutOfBoundsException(len - 0);
}
System.arraycopy(str.value, 0, value, value.length, len - 0);

count += len;
return this;

After removing code that will never execute with the given string, and removing code that is the same between them:

//////////////concat

int len = value.length;
len + otherLen
System.arraycopy(value, 0, buf, 0, Math.min(value.length, len + otherLen));
System.arraycopy(str.value, 0, buf, len, str.value.length);
this.value = value;

/////////////////append

if(value.length + len - value.length > 0)
int newCapacity = value.length * 2 + 2;
if(newCapacity - value.length + len < 0)
if(newCapacity < 0)
System.arraycopy(value, 0, value, 0, (value.length <= newCapacity) ? value.length : newCapacity);
if(0 < 0)
if(len > str.value.length)
if(0 > len)
System.arraycopy(str.value, 0, value, value.length, len - 0);
count += len;

After counting all the operations and removing operations that are the same between concat and append:

concat
--------
int assignment: 0
int +/-: 0
int comparison: 0
char[] assignment: 1
arraycopy: 0
int *: 0


append
--------
int assignment: 1
int +/-: 5
int comparison: 6
char[] assignment: 0
arraycopy: 0
int *: 1

You can see that one concat will be faster than one append in almost all cases, and + compiles to two appends and a toString.


1: A: String concatenation: concat() vs + operator

like image 64
The Guy with The Hat Avatar answered Sep 18 '22 21:09

The Guy with The Hat