Почему ".concat(String)" намного быстрее, чем "+"? - программирование
Подтвердить что ты не робот

Почему ".concat(String)" намного быстрее, чем "+"?

В некотором коде, который я сделал, сравнивается время, затрачиваемое на объединение строк с помощью "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);
}

Где str == "string".

Выход, который я получаю, последовательно похож на этот, хотя средняя разница, как правило, ближе к 61 наносекундам:

String str2 = str + str: 118.57349468 наносекунды

String str2 = str.concat(str): 52.36809985 наносекунды

.concat быстрее, чем + на 66.20539483 наносекунды

Это показывает, что даже с циклом и назначением новой строки .concat быстрее, чем + более чем на два. Когда я использую еще более длинную строку (str == "this is a really really very long string that is very long"), она быстрее примерно в три раза. Это особенно странно, потому что если .concat работает быстрее, не следует ли компилировать + на .concat?

Мой главный вопрос: Почему .concat быстрее?

Полный код, если вы хотите запустить его и поэкспериментировать с ним:

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");
    }
}
4b9b3361

Ответ 1

Изменить: Оглядываясь назад на этот ответ, я теперь понимаю, насколько он ненаучный и умозрительный. Хотя это не обязательно неправильно, я больше не уверен в его правильности.


Вот исходный код для 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" компилируется в new StringBuilder().append("string").append("string").toString(). 1append источник использует свой суперкласс ', 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;
}

После замены вызова метода с помощью источника метода:

/////////////////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;

После удаления кода, который никогда не будет выполняться с данной строкой, и удаления между ними одинакового кода:

//////////////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;

После подсчета всех операций и операций удаления, которые совпадают между concat и 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

Вы можете видеть, что один concat будет быстрее, чем один append почти во всех случаях, а + скомпилируется в два приложения и toString.


1: A: Конкатенация строк: concat() vs + operator