3

アルゴリズムの一部について意見やアドバイスを求めたいと思います。

ByteBuffer bb = ByteBuffer.allocate(8);
bb.putLong(rs.getLong(index));//retrieve long from db (unsigned INT)
byte[] tmp = new byte[4];
bb.position(4);
bb.get(tmp);
(Inet4Address) InetAddress.getByAddress(tmp);

対。

ByteBuffer bb = ByteBuffer.allocate(4);
bb.putInt((int) rs.getLong(index));//retrieve long from db (unsigned INT)
bb.flip();
byte[] tmp = new byte[4];
bb.get(tmp);
(Inet4Address) InetAddress.getByAddress(tmp);

基本的に、キャストにパフォーマンスの違いがあるのか​​ 、それともより大きなByteBufferを使用する方が良いのかを知りたい.

ありがとうございます。それでは、お元気で、

マレク

4

1 に答える 1

2

Basically I would like to know whether there is a performance difference in casting or is it better to use bigger ByteBuffer.

ByteBufferキャストは、特に newの割り当てやいくつかのメソッドの呼び出しと比較して「安価」です。

あなたが何をしようとしているのか完全にはわかりませんが、おそらく単純な右シフトでうまくいくでしょうか? たとえば、次のコード スニペット:

long l = rs.getLong(index);
InetAddress.getByAddress(new byte[] { (byte) ((l & 0xFF000000) >> 24),
                                      (byte) ((l & 0x00FF0000) >> 16),
                                      (byte) ((l & 0x0000FF00) >>  8),
                                      (byte) ((l & 0x000000FF) >>  0)});
于 2011-02-10T14:00:34.687 に答える