6

次のコードを実行すると、Java String のネイティブ メソッドgetBytes()の実行が、カスタムのgetBytesFast()実装よりも遅いように見えます。を使用して、両方のバイト配列が等しいことを確認できます。Arrays.equals(str.getBytes(), getBytesFast(str))

getBytesFast の実装は、このプログラミングのヒントの記事 (1997 年) に含まれている実装の修正版です: http://java.sun.com/developer/technicalArticles/Programming/Performance/

ネイティブ実装がカスタム実装よりも遅い理由について、十分に文書化された回答を探しています。

package com.test;

public class Performance {

    public static void main(String args[]) {

        final String str = "This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test! This is a performance test!";

        long startTime_1 = System.nanoTime();
        str.getBytes();
        System.out.println(System.nanoTime() - startTime_1);

        long startTime_2 = System.nanoTime();
        getBytesFast(str);
        System.out.println(System.nanoTime() - startTime_2);
    }

    private static byte[] getBytesFast(String str) {
        final char buffer[] = new char[str.length()];
        final int length = str.length();
        str.getChars(0, length, buffer, 0);
        final byte b[] = new byte[length];
        for (int j = 0; j < length; j++)
            b[j] = (byte) buffer[j];
        return b;
    }
}

編集:

キャリパーのベンチマーク結果

ここに画像の説明を入力

ありがとう

4

3 に答える 3

13

[ドキュメント]によるとchar、Javaの型は16ビットのUnicode文字ですが、byte型は8ビットの符号付き整数です。これは、コードで作成されたキャストごとに、文字データの半分をchar破棄することを意味します。byte

文字ストリームとバイトストリームに関するJavaチュートリアルには、日本語の漢字を使用した文字列の例があります。

String jaString = new String("\u65e5\u672c\u8a9e\u6587\u5b57\u5217");

その文字列の各文字について、高速変換メソッドは情報の最初のバイト(たとえば、65in \u65e5)を破棄します。あなたのリンクはまた、「前者は文字ごとの関数呼び出しを含むバイトから文字への変換を修正するため」String.getBytes()、実行速度が数倍遅くなることを具体的に述べています。

文字エンコードを完全に無視し、それぞれから上位バイトをchar破棄すると、少しスピードアップします。この方法は特定の文字エンコードでのみ機能し、データが失われる可能性があることを覚えておく必要があります。

于 2012-09-02T22:17:06.203 に答える
12

String.getBytesシステムのデフォルトの文字セットを考慮します。実装はISO-8859-1を想定しています。

String.getBytes最終的にこのメソッドを呼び出すことになります。ceですCharsetEncoder

byte[] encode(char[] ca, int off, int len) {
        int en = scale(len, ce.maxBytesPerChar());
        byte[] ba = new byte[en];
        if (len == 0)
            return ba;
        if (ce instanceof ArrayEncoder) {
            int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
            return safeTrim(ba, blen, cs, isTrusted);
        } else {
            ce.reset();
            ByteBuffer bb = ByteBuffer.wrap(ba);
            CharBuffer cb = CharBuffer.wrap(ca, off, len);
            try {
                CoderResult cr = ce.encode(cb, bb, true);
                if (!cr.isUnderflow())
                    cr.throwException();
                cr = ce.flush(bb);
                if (!cr.isUnderflow())
                    cr.throwException();
            } catch (CharacterCodingException x) {
                // Substitution is always enabled,
                // so this shouldn't happen
                throw new Error(x);
            }
            return safeTrim(ba, bb.position(), cs, isTrusted);
        }
    }
}

private static int scale(int len, float expansionFactor) {
    // We need to perform double, not float, arithmetic; otherwise
    // we lose low order bits when len is larger than 2**24.
    return (int)(len * (double)expansionFactor);
}

private static char[] safeTrim(char[] ca, int len,
                               Charset cs, boolean isTrusted) {
    if (len == ca.length && (isTrusted || System.getSecurityManager() == null))
        return ca;
    else
        return Arrays.copyOf(ca, len);
}

を使用すると複雑さが大幅に増し、実行時間が遅くなる原因とCharsetEncoderなる可能性があります。

于 2012-09-02T21:59:11.883 に答える
4

これはString.getBytes()、Charset (JVM の現在のデフォルト) を使用または委譲し、「高速」実装がハードコーディングされた ISO-8859-1 文字セットであることが原因である可能性があります。

(注:私はあなたの結果を検証しませんでした。私はここで私の仮説を述べているだけです。マイクロベンチマークに関するコメントはここでは重要ではなく、あなたの質問に対する私の答えよりも間違いなく価値があります:)

于 2012-09-02T21:53:24.907 に答える