为什么本地String getBytes方法比自定义实现的getBytesFast慢? - java

运行以下代码时,执行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;
    }
}

编辑:

卡尺基准测试结果

谢谢

参考方案

String.getBytes 考虑了系统的默认字符集。您的实现采用ISO-8859-1。
String.getBytes最终最终调用此方法。 ceCharsetEncoder

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涉及更大的复杂度,这可能导致您看到的执行时间变慢。

Java 7中通用类的变量访问权限的更改 - java

这是一些使用Java 6编译但未在Java 7中编译的代码的简单示例。public class Test<T extends Test> { private final int _myVar; public Test(int myVar) { _myVar = myVar; } public int get(TestContainer<T&…

Java AWT性能焦虑 - java

有一个程序可以一次在屏幕上生成数百或数千个运动粒子。显示几百后,就会出现减速。使用netbeans探查器分析了性能,大约80%的时间都花在了jpanel的绘制方法上……因此算法优化似乎不太可能产生明显的变化。有什么可以做的吗?还是该考虑一个新平台了? paint方法看起来像这样:public void paintComponent(Graphics g) {…

Java中的精确时间测量 - java

Java提供了两种获取当前时间的方法:System.nanoTime()和System.currentTimeMillis()。第一个给出的结果以纳秒为单位,但是实际精度要差得多(许多微秒)。JVM是否已经为每台特定的机器提供了最佳的价值?否则,是否有一些Java库可以通过与特定系统绑定来进行更精细的测量? 参考方案 获得超精确时间测量的问题在于某些处理器无…

Java无法及时收集垃圾 - java

我有一个问题,jvm无法及时执行gc并冻结了应用程序。为此,“解决方案”是使用jconsole连接到应用程序,并建议jvm进行垃圾回收。我不必说这是应用程序的不良行为。 jvm是否有一些建议建议它更快/更频繁地执行gc?也许对此问题还有其他一些真正的解决方案?问题似乎是内存不足,但是gc在将新数据发送到应用程序之前无法及时进行收集。之所以如此,是因为gc似乎…

Hibernate是否对Android应用程序构成过大杀伤力? - java

我正在为我的android应用程序寻找一个好的ORM,乍一看,似乎对于移动设备,我希望使用更简单的方法。问题是,我只是在这里假设没有真实的证据,所以我想问一下社区的意见(也许有人经历过这种经历)。它是一个相当大的(对于移动设备)应用程序,将在专用平板电脑上运行。其他人怎么想?对于Android应用程序来说,Hibernate太多了吗?会有性能问题吗?如果您认…