Java 中字节数组到 29 位整数的最快且最有效的转换

发布于 2024-09-17 21:18:00 字数 692 浏览 1 评论 0原文

由于 29 位整数在 AMF 中很流行,我想合并已知的最快/最好的例程。我们的库中当前存在两个例程,可以在 ideone 上进行实时测试 http://ideone.com/KNmYT
这是快速参考的来源 <代码>

public static int readMediumInt(ByteBuffer in) {
    ByteBuffer buf = ByteBuffer.allocate(4);
    buf.put((byte) 0x00);
    buf.put(in.get());
    buf.put(in.get());
    buf.put(in.get());
    buf.flip();
    return buf.getInt();
}

公共静态 int readMediumInt2(ByteBuffer in) { byte[] 字节 = 新字节[3]; in.get(字节); 整数值=0; val += 字节[0] * 256 * 256; val += 字节[1] * 256; val += 字节[2]; 如果(值<0){ 值 += 256; } 返回值; }

Since 29 bit integers are popular in AMF, I would like to incorporate the fastest / best routine known. Two routines currently exist in our library and can be tested live on ideone
http://ideone.com/KNmYT

Here is the source for quick reference

public static int readMediumInt(ByteBuffer in) {
    ByteBuffer buf = ByteBuffer.allocate(4);
    buf.put((byte) 0x00);
    buf.put(in.get());
    buf.put(in.get());
    buf.put(in.get());
    buf.flip();
    return buf.getInt();
}

public static int readMediumInt2(ByteBuffer in) { byte[] bytes = new byte[3]; in.get(bytes); int val = 0; val += bytes[0] * 256 * 256; val += bytes[1] * 256; val += bytes[2]; if (val < 0) { val += 256; } return val; }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柏拉图鍀咏恒 2024-09-24 21:18:00

通常我会通过位运算来完成此操作。第二个版本最终可能会被 JVM 优化到接近此的程度,但无法确定。现在,按照您的示例,这只是 24 位,但问题是“29 位整数”。我不确定你真正想要哪个。

public static int readMediumInt(ByteBuffer buf) {
  return ((buf.get() & 0xFF) << 16) 
       | ((buf.get() & 0xFF) <<  8)
       | ((buf.get() & 0xFF);
}

Usually I'd do this with bit operations. The second version might eventually get optimized to something close to this by the JVM, but one can't be sure. Now, this is only 24 bits, following your samples, but the question says "29 bit integer". I'm not sure which you really wanted.

public static int readMediumInt(ByteBuffer buf) {
  return ((buf.get() & 0xFF) << 16) 
       | ((buf.get() & 0xFF) <<  8)
       | ((buf.get() & 0xFF);
}
咆哮 2024-09-24 21:18:00

如果您确实想读取 AMF 29 位整数,这应该可以完成工作(假设我已经正确理解了格式):

private static int readMediumInt(ByteBuffer buf) {
    int b0, b1, b2;
    if ((b0 = buf.get()) >= 0) return b0;
    if ((b1 = buf.get()) >= 0) return ((b0 << 7) & ((~(-1 << 7)) << 7)) | b1;
    if ((b2 = buf.get()) >= 0) return ((b0 << 14) & ((~(-1 << 7)) << 14)) | ((b1 << 7) & ((~(-1 << 7)) << 7)) | b2;
    return ((b0 << 22) & ((~(-1 << 7)) << 22)) | ((b1 << 15) & ((~(-1 << 7)) << 15)) | ((b2 << 8) & ((~(-1 << 7)) << 8)) | (buf.get() & 0xff);
}

If you do actually want to read AMF 29-bit integers, this should do the job (assuming i've understood the format correctly):

private static int readMediumInt(ByteBuffer buf) {
    int b0, b1, b2;
    if ((b0 = buf.get()) >= 0) return b0;
    if ((b1 = buf.get()) >= 0) return ((b0 << 7) & ((~(-1 << 7)) << 7)) | b1;
    if ((b2 = buf.get()) >= 0) return ((b0 << 14) & ((~(-1 << 7)) << 14)) | ((b1 << 7) & ((~(-1 << 7)) << 7)) | b2;
    return ((b0 << 22) & ((~(-1 << 7)) << 22)) | ((b1 << 15) & ((~(-1 << 7)) << 15)) | ((b2 << 8) & ((~(-1 << 7)) << 8)) | (buf.get() & 0xff);
}
折戟 2024-09-24 21:18:00

最重要的变化是避免在方法内分配对象。顺便说一句,您的微基准测试没有重置“开始”,因此第二个结果包括第一个方法所用的时间。另外,您需要多次运行微基准测试,否则即时编译器没有机会运行。我建议使用类似的方法

public static int readMediumInt3(ByteBuffer buf) {
    return ((buf.get() & 0xff) << 16) + 
            ((buf.get() & 0xff) << 8) + 
            ((buf.get() & 0xff));
}

完整的代码是:

import java.nio.ByteBuffer;

public class Main {

    public static int readMediumInt(ByteBuffer in) {
        ByteBuffer buf = ByteBuffer.allocate(4);
        buf.put((byte) 0x00);
        buf.put(in.get());
        buf.put(in.get());
        buf.put(in.get());
        buf.flip();
        return buf.getInt();
    }

    public static int readMediumInt2(ByteBuffer in) {
        byte[] bytes = new byte[3];
        in.get(bytes);
        int val = 0;
        val += bytes[0] * 256 * 256;
        val += bytes[1] * 256;
        val += bytes[2];
        if (val < 0) {
            val += 256;
        }
        return val;
    }

    public static int readMediumInt3(ByteBuffer buf) {
        return ((buf.get() & 0xff) << 16) + 
                ((buf.get() & 0xff) << 8) + 
                ((buf.get() & 0xff));
    }

    public static void main(String[] args) {
        Main m = new Main();
        for (int i = 0; i < 5; i++) {
            // version 1
            ByteBuffer buf = ByteBuffer.allocate(4);
            buf.putInt(424242);
            buf.flip();
            long start;
            start = System.nanoTime();
            for (int j = 0; j < 10000000; j++) {
                buf.position(0);
                readMediumInt(buf);
            }
            start = System.nanoTime() - start;
            System.out.printf("Ver 1: elapsed: %d ms\n", start / 1000000);

            // version 2
            ByteBuffer buf2 = ByteBuffer.allocate(4);
            buf2.putInt(424242);
            buf2.flip();
            start = System.nanoTime();
            for (int j = 0; j < 10000000; j++) {
                buf2.position(0);
                readMediumInt2(buf2);
            }
            start = System.nanoTime() - start;
            System.out.printf("Ver 2: elapsed: %d ms\n", start / 1000000);

            // version 3
            ByteBuffer buf3 = ByteBuffer.allocate(4);
            buf3.putInt(424242);
            buf3.flip();
            start = System.nanoTime();
            for (int j = 0; j < 10000000; j++) {
                buf3.position(0);
                readMediumInt3(buf3);
            }
            start = System.nanoTime() - start;
            System.out.printf("Ver 3: elapsed: %d ms\n", start / 1000000);
        }

    }
}

我的结果:

  • Ver 1: elapsed: 556 ms
  • Ver 2: elapsed: 187 ms
  • Ver 3: elapsed: 3 ms

The most important change is to avoid allocating objects within the method. By the way your micro benchmark didn't reset "start", so the second result includes the time used for the first method. Also, you need to run micro benchmarks multiple times, otherwise the just in time compiler has no chance to run. I suggest to use a method similar to

public static int readMediumInt3(ByteBuffer buf) {
    return ((buf.get() & 0xff) << 16) + 
            ((buf.get() & 0xff) << 8) + 
            ((buf.get() & 0xff));
}

The complete code is:

import java.nio.ByteBuffer;

public class Main {

    public static int readMediumInt(ByteBuffer in) {
        ByteBuffer buf = ByteBuffer.allocate(4);
        buf.put((byte) 0x00);
        buf.put(in.get());
        buf.put(in.get());
        buf.put(in.get());
        buf.flip();
        return buf.getInt();
    }

    public static int readMediumInt2(ByteBuffer in) {
        byte[] bytes = new byte[3];
        in.get(bytes);
        int val = 0;
        val += bytes[0] * 256 * 256;
        val += bytes[1] * 256;
        val += bytes[2];
        if (val < 0) {
            val += 256;
        }
        return val;
    }

    public static int readMediumInt3(ByteBuffer buf) {
        return ((buf.get() & 0xff) << 16) + 
                ((buf.get() & 0xff) << 8) + 
                ((buf.get() & 0xff));
    }

    public static void main(String[] args) {
        Main m = new Main();
        for (int i = 0; i < 5; i++) {
            // version 1
            ByteBuffer buf = ByteBuffer.allocate(4);
            buf.putInt(424242);
            buf.flip();
            long start;
            start = System.nanoTime();
            for (int j = 0; j < 10000000; j++) {
                buf.position(0);
                readMediumInt(buf);
            }
            start = System.nanoTime() - start;
            System.out.printf("Ver 1: elapsed: %d ms\n", start / 1000000);

            // version 2
            ByteBuffer buf2 = ByteBuffer.allocate(4);
            buf2.putInt(424242);
            buf2.flip();
            start = System.nanoTime();
            for (int j = 0; j < 10000000; j++) {
                buf2.position(0);
                readMediumInt2(buf2);
            }
            start = System.nanoTime() - start;
            System.out.printf("Ver 2: elapsed: %d ms\n", start / 1000000);

            // version 3
            ByteBuffer buf3 = ByteBuffer.allocate(4);
            buf3.putInt(424242);
            buf3.flip();
            start = System.nanoTime();
            for (int j = 0; j < 10000000; j++) {
                buf3.position(0);
                readMediumInt3(buf3);
            }
            start = System.nanoTime() - start;
            System.out.printf("Ver 3: elapsed: %d ms\n", start / 1000000);
        }

    }
}

My results:

  • Ver 1: elapsed: 556 ms
  • Ver 2: elapsed: 187 ms
  • Ver 3: elapsed: 3 ms
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文