由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - byte[] to int[]
相关主题
大文件的download如何管理?How to parse the bytes[]
如何避免java web start读取资源文件读了多次JSP is rubbish!
求教一个Java问题 IllegalMonitorStateExceptionhow to update swing components
弱人再问一个关于Java的问题how to print starting from specific pos?
问个很简单的问题?a stupid question
help about bitstream writerAn interesting thing about java generics-do not laugh at me if u think it too basic
怎样吧byte[]变成java.security.Key?Re: native thread 和green thread
怎麼得到字符串中的raw bytes?Re: 问一个关于Java Native Interface的问题
相关话题的讨论汇总
话题: int话题: 0xff话题: bytebuffer话题: buf话题: byte
进入Java版参与讨论
1 (共1页)
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,所以是希望尽可能的快 ...

1 (共1页)
进入Java版参与讨论
相关主题
Re: 问一个关于Java Native Interface的问题问个很简单的问题?
Read this about JApplethelp about bitstream writer
Core Java2 Notes (6)怎样吧byte[]变成java.security.Key?
jar 的问题, 路径有问题? 大家帮忙看看是啥毛病?怎麼得到字符串中的raw bytes?
大文件的download如何管理?How to parse the bytes[]
如何避免java web start读取资源文件读了多次JSP is rubbish!
求教一个Java问题 IllegalMonitorStateExceptionhow to update swing components
弱人再问一个关于Java的问题how to print starting from specific pos?
相关话题的讨论汇总
话题: int话题: 0xff话题: bytebuffer话题: buf话题: byte