使用Android Profiler工具,我發現我在代碼的性能關鍵區域使用了大量的ByteBuffers。我一直在儘可能多地優化最大的性能命中(還沒有被迫去做像循環優化那樣的東西),而且ByteBuffer代碼看起來像可以很容易地被另一個/更好的解決方案所取代。在這種情況下替換Java的ByteBuffer?
的問題:獲取從位於字節緩衝區流中的4個連續的字節,其可以是任何長度的一個int,目前進行(下面看到的那樣,具體地// < < <份)與一個臨時的ByteBuffer,抓住需要字節組成一個int。有沒有更簡單的過程?
private int peekStreamData(ByteBuffer stream, int count) {
int data = 0;
int stream_field = streamField;
int stream_field_bit_index = streamFieldBitIndex;
byte[] streamArr = stream.array();
ByteBuffer bb = ByteBuffer.allocate(4);
while (count > (32 - stream_field_bit_index) && streamIndex < (imageStream.capacity() >> 2)) {
data = (data << (32 - stream_field_bit_index)) | (stream_field >>> stream_field_bit_index);
count -= 32 - stream_field_bit_index;
bb.put(0,streamArr[streamIndex * 4 + 3]); //<<<
bb.put(1,streamArr[streamIndex * 4 + 2]); //<<<
bb.put(2,streamArr[streamIndex * 4 + 1]); //<<<
bb.put(3,streamArr[streamIndex * 4 + 0]); //<<<
stream_field = bb.getInt(); //<<<
stream_field_bit_index = 0;
}
if (count > 0)
data = (data << count) | (stream_field >>> (32 - count));
return data;//data;
}
請隨時指出任何錯誤。直到最近,我從未與ByteBuffers合作過,並且很願意學習。
我將這兩個值與我當前的代碼進行了比較,它們不輸出相同的輸入值。也許你有一個錯誤? –
啊,是的,你編輯的代碼看起來和我目前使用的非常相似。我認爲這是缺失的部分。謝謝您的幫助。 –