Is there is a faster alternative for Integer.toString(myInt).getBytes(US_ASCII)?

Dmitriy Dumanskiy :

User sends me byte / short / int / long value. I have to send it as the part of POST HTTP request and I have to send number as String.

So right now I do the next:

//simplified version
byte[] data = Integer.toString(myInt).getBytes(US_ASCII);
sendPost(data);

I'm looking for faster alternative for

Integer.toString(myInt).getBytes(US_ASCII);

Because this flow creates char[], String and byte[] objects. While I need only byte[]. I wonder if there is any faster/better alternative to this.

Dmitriy Dumanskiy :

Thank you all for the suggestions. I decided to go through the easiest possible way. I just copied the methods from the Integer.toString() and inlined into my code with the minimal modifications. I didn't perform exact benchmark, however, for JDBC driver I writing I got 2x performance boost due to this change. Here is the final solution:

private static final byte[] DigitTens = {
        '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
        '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
        '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
        '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
        '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
        '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
        '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
        '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
        '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
        '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
};

private static final byte[] DigitOnes = {
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
};

public byte[] intAsStringToBytes(int i) {
    int size = stringSize(i);
    byte[] buf = new byte[size];
    getChars(i, size, buf);
    return buf;
}

private static int stringSize(int x) {
    int d = 1;
    if (x >= 0) {
        d = 0;
        x = -x;
    }
    int p = -10;
    for (int i = 1; i < 10; i++) {
        if (x > p) {
            return i + d;
        }
        p = 10 * p;
    }
    return 10 + d;
}

private static void getChars(int i, int index, byte[] buf) {
    int q, r;
    int charPos = index;

    boolean negative = i < 0;
    if (!negative) {
        i = -i;
    }

    // Generate two digits per iteration
    while (i <= -100) {
        q = i / 100;
        r = (q * 100) - i;
        i = q;
        buf[--charPos] = DigitOnes[r];
        buf[--charPos] = DigitTens[r];
    }

    // We know there are at most two digits left at this point.
    q = i / 10;
    r = (q * 10) - i;
    buf[--charPos] = (byte) ('0' + r);

    // Whatever left is the remaining digit.
    if (q < 0) {
        buf[--charPos] = (byte) ('0' - q);
    }

    if (negative) {
        buf[--charPos] = (byte) '-';
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=138907&siteId=1