j******n 发帖数: 108 | 1 很简单的一个问题,把 byte[] 转换成相应的 int[]
我写了一个循环,每次转换 4byte
因为需要转换的数量可能比较大,有几十万的int
我测了一下时间,还是不大能满足我的要求
请问,还有没有比较优化的方法么?比如类似 c 里面的 memcp
4byte 到 int 的转换如下:
int x = ((buf[start] & 0xFF) << 24)
| ((buf[start+1] & 0xFF) << 16)
| ((buf[start+2] & 0xFF) << 8)
| (buf[start+3] & 0xFF);
还是说,大概只能达到这个水平,很难再优化了?
非常感谢! |
c*****t 发帖数: 1879 | 2 只有这个办法。应该没那么慢的。几十万的应该几乎是 real time 。
估计是你其他地方没弄好。
的转换如下:
【在 j******n 的大作中提到】 : 很简单的一个问题,把 byte[] 转换成相应的 int[] : 我写了一个循环,每次转换 4byte : 因为需要转换的数量可能比较大,有几十万的int : 我测了一下时间,还是不大能满足我的要求 : 请问,还有没有比较优化的方法么?比如类似 c 里面的 memcp : 4byte 到 int 的转换如下: : int x = ((buf[start] & 0xFF) << 24) : | ((buf[start+1] & 0xFF) << 16) : | ((buf[start+2] & 0xFF) << 8) : | (buf[start+3] & 0xFF);
|
Z****e 发帖数: 2999 | 3 try ByteBuffer:
ByteBuffer buf = ByteBuffer.wrap(byteArray);
buf.order(endianess); //set endianess
IntBuffer intBuf = buf.asIntBuffer(); //check for null
int[] intArray = intBuf.array();
的转换如下:
【在 j******n 的大作中提到】 : 很简单的一个问题,把 byte[] 转换成相应的 int[] : 我写了一个循环,每次转换 4byte : 因为需要转换的数量可能比较大,有几十万的int : 我测了一下时间,还是不大能满足我的要求 : 请问,还有没有比较优化的方法么?比如类似 c 里面的 memcp : 4byte 到 int 的转换如下: : int x = ((buf[start] & 0xFF) << 24) : | ((buf[start+1] & 0xFF) << 16) : | ((buf[start+2] & 0xFF) << 8) : | (buf[start+3] & 0xFF);
|
c*****t 发帖数: 1879 | 4 Actually, using ByteBuffer could only get worse since function
calls are extremely expensive comparing to the operations he
mentioned.
【在 Z****e 的大作中提到】 : try ByteBuffer: : ByteBuffer buf = ByteBuffer.wrap(byteArray); : buf.order(endianess); //set endianess : IntBuffer intBuf = buf.asIntBuffer(); //check for null : int[] intArray = intBuf.array(); : : 的转换如下:
|
j******n 发帖数: 108 | 5 其实我倒不一定非要转换成 int[]
基本目的就是后面的操作要把这些int一个个读出来,做一些运算。
我看 IntBuffer 也提供了 get put 之类的方法,不知道背后的原理
是怎么样的。最后结果够快就好。我测测吧。
【在 c*****t 的大作中提到】 : Actually, using ByteBuffer could only get worse since function : calls are extremely expensive comparing to the operations he : mentioned.
|
j******n 发帖数: 108 | 6 您指的 real time 是多快?
我测了一下,转换4万 int 就要大概8ms
还是这已经足够快了?
=)
主要我要和别人 compete,所以是希望尽可能的快 ...
【在 c*****t 的大作中提到】 : 只有这个办法。应该没那么慢的。几十万的应该几乎是 real time 。 : 估计是你其他地方没弄好。 : : 的转换如下:
|
Z****e 发帖数: 2999 | 7 never read about its performance and stuff, but I remeber the document says
it's supposed to use native ops as much as possible to provide high
performance io?
【在 c*****t 的大作中提到】 : Actually, using ByteBuffer could only get worse since function : calls are extremely expensive comparing to the operations he : mentioned.
|
Z****e 发帖数: 2999 | 8 did a test program, indeed ByteBuffer is slower
looked into ByteBuffer's source, I thought they were using native methods to
back most ops, looks like not; guess I have misread the java doc...
says
【在 Z****e 的大作中提到】 : never read about its performance and stuff, but I remeber the document says : it's supposed to use native ops as much as possible to provide high : performance io?
|
m******t 发帖数: 2416 | 9
That's not too bad...
If you really need to push it, I guess the
only other option is doing it in a native
method.
【在 j******n 的大作中提到】 : 您指的 real time 是多快? : 我测了一下,转换4万 int 就要大概8ms : 还是这已经足够快了? : =) : 主要我要和别人 compete,所以是希望尽可能的快 ...
|