replaced integer * and % with shift operations, for performance

This commit is contained in:
Tammo Spalink
2009-04-15 19:15:37 +08:00
parent 967f7c169c
commit e564b19ed2
3 changed files with 39 additions and 14 deletions

View File

@@ -51,7 +51,7 @@ public class BitwiseInputStream {
*/
public BitwiseInputStream(byte buf[]) {
mBuf = buf;
mEnd = buf.length * 8;
mEnd = buf.length << 3;
mPos = 0;
}
@@ -70,13 +70,13 @@ public class BitwiseInputStream {
* @return byte of read data (possibly partially filled, from lsb)
*/
public byte read(int bits) throws AccessException {
int index = mPos / 8;
int offset = 16 - (mPos % 8) - bits;
int index = mPos >>> 3;
int offset = 16 - (mPos & 0x07) - bits; // &7==%8
if ((bits < 0) || (bits > 8) || ((mPos + bits) > mEnd)) {
throw new AccessException("illegal read " +
"(pos " + mPos + ", end " + mEnd + ", bits " + bits + ")");
}
int data = (mBuf[index] & 0xFF) << 8;
int data = (mBuf[index] & 0x00FF) << 8;
if (offset < 8) data |= (mBuf[index + 1] & 0xFF);
data >>>= offset;
data &= (-1 >>> (32 - bits));
@@ -92,10 +92,10 @@ public class BitwiseInputStream {
* @return newly allocated byte array of read data
*/
public byte[] readByteArray(int bits) throws AccessException {
int bytes = (bits / 8) + ((bits % 8) > 0 ? 1 : 0);
int bytes = (bits >>> 3) + ((bits & 0x07) > 0 ? 1 : 0); // &7==%8
byte[] arr = new byte[bytes];
for (int i = 0; i < bytes; i++) {
int increment = Math.min(8, bits - (i * 8));
int increment = Math.min(8, bits - (i << 3));
arr[i] = (byte)(read(increment) << (8 - increment));
}
return arr;

View File

@@ -51,7 +51,7 @@ public class BitwiseOutputStream {
*/
public BitwiseOutputStream(int startingLength) {
mBuf = new byte[startingLength];
mEnd = startingLength * 8;
mEnd = startingLength << 3;
mPos = 0;
}
@@ -61,7 +61,7 @@ public class BitwiseOutputStream {
* @return newly allocated byte array
*/
public byte[] toByteArray() {
int len = (mPos / 8) + ((mPos % 8) > 0 ? 1 : 0);
int len = (mPos >>> 3) + ((mPos & 0x07) > 0 ? 1 : 0); // &7==%8
byte[] newBuf = new byte[len];
System.arraycopy(mBuf, 0, newBuf, 0, len);
return newBuf;
@@ -74,8 +74,8 @@ public class BitwiseOutputStream {
*/
private void possExpand(int bits) {
if ((mPos + bits) < mEnd) return;
byte[] newBuf = new byte[((mPos + bits) * 2) / 8];
System.arraycopy(mBuf, 0, newBuf, 0, mEnd / 8);
byte[] newBuf = new byte[(mPos + bits) >>> 2];
System.arraycopy(mBuf, 0, newBuf, 0, mEnd >>> 3);
mBuf = newBuf;
}
@@ -91,12 +91,12 @@ public class BitwiseOutputStream {
}
possExpand(bits);
data &= (-1 >>> (32 - bits));
int index = mPos / 8;
int offset = 16 - (mPos % 8) - bits;
int index = mPos >>> 3;
int offset = 16 - (mPos & 0x07) - bits; // &7==%8
data <<= offset;
mPos += bits;
mBuf[index] |= (data >>> 8);
if (offset < 8) mBuf[index + 1] |= (data & 0xFF);
if (offset < 8) mBuf[index + 1] |= (data & 0x00FF);
}
/**
@@ -107,7 +107,7 @@ public class BitwiseOutputStream {
*/
public void writeByteArray(int bits, byte[] arr) throws AccessException {
for (int i = 0; i < arr.length; i++) {
int increment = Math.min(8, bits - (i * 8));
int increment = Math.min(8, bits - (i << 3));
if (increment > 0) {
write(increment, (byte)(arr[i] >>> (8 - increment)));
}

View File

@@ -23,6 +23,8 @@ import com.android.internal.util.HexDump;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;
public class BitwiseStreamsTest extends AndroidTestCase {
private final static String LOG_TAG = "BitwiseStreamsTest";
@@ -85,4 +87,27 @@ public class BitwiseStreamsTest extends AndroidTestCase {
for (int i = 0; i < inBufDup.length; i++) inBufDup[i] = inStream.read(8);
assertEquals(HexDump.toHexString(inBuf), HexDump.toHexString(inBufDup));
}
@SmallTest
public void testFive() throws Exception {
int num_runs = 10;
long start = android.os.SystemClock.elapsedRealtime();
for (int run = 0; run < num_runs; run++) {
int offset = run % 8;
byte[] inBuf = HexDump.hexStringToByteArray("00031040900112488ea794e0");
BitwiseOutputStream outStream = new BitwiseOutputStream(30);
outStream.skip(offset);
for (int i = 0; i < inBuf.length; i++) {
outStream.write(5, inBuf[i] >>> 3);
outStream.write(3, inBuf[i] & 0x07);
}
BitwiseInputStream inStream = new BitwiseInputStream(outStream.toByteArray());
inStream.skip(offset);
byte[] inBufDup = new byte[inBuf.length];
for (int i = 0; i < inBufDup.length; i++) inBufDup[i] = inStream.read(8);
assertEquals(HexDump.toHexString(inBuf), HexDump.toHexString(inBufDup));
}
long end = android.os.SystemClock.elapsedRealtime();
Log.d(LOG_TAG, "repeated encode-decode took " + (end - start) + " ms");
}
}