409953 More efficient implementation of BufferUtil.writeTo(...)
This commit is contained in:
parent
7792c1dad7
commit
928275271d
|
@ -84,7 +84,7 @@ import java.nio.charset.Charset;
|
|||
*/
|
||||
public class BufferUtil
|
||||
{
|
||||
static final int TEMP_BUFFER_SIZE = 4096;
|
||||
static final int TEMP_BUFFER_SIZE = 512;
|
||||
static final byte SPACE = 0x20;
|
||||
static final byte MINUS = '-';
|
||||
static final byte[] DIGIT =
|
||||
|
@ -439,18 +439,11 @@ public class BufferUtil
|
|||
else
|
||||
{
|
||||
byte[] bytes = new byte[TEMP_BUFFER_SIZE];
|
||||
int j = 0;
|
||||
for (int i = buffer.position(); i < buffer.limit(); i++)
|
||||
{
|
||||
bytes[j] = buffer.get(i);
|
||||
j++;
|
||||
if (j == TEMP_BUFFER_SIZE)
|
||||
{
|
||||
out.write(bytes);
|
||||
j = 0;
|
||||
}
|
||||
while(buffer.hasRemaining()){
|
||||
int byteCountToWrite = Math.min(buffer.remaining(), TEMP_BUFFER_SIZE);
|
||||
buffer.get(bytes, 0, byteCountToWrite);
|
||||
out.write(bytes,0 , byteCountToWrite);
|
||||
}
|
||||
out.write(bytes, 0, j);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -241,24 +241,30 @@ public class BufferUtilTest
|
|||
"ignored.")
|
||||
public void testWriteToMicrobenchmark() throws IOException
|
||||
{
|
||||
int capacity = 1024 * 1024;
|
||||
int iterations = 30;
|
||||
int capacity = 1024 * 128;
|
||||
int iterations = 100;
|
||||
int testRuns = 10;
|
||||
byte[] bytes = new byte[capacity];
|
||||
new Random().nextBytes(bytes);
|
||||
ByteBuffer buffer = BufferUtil.allocate(capacity);
|
||||
BufferUtil.append(buffer, bytes, 0, capacity);
|
||||
long start = System.nanoTime();
|
||||
for (int i = 0; i < iterations; i++)
|
||||
long startTest = System.nanoTime();
|
||||
for (int i = 0; i < testRuns; i++)
|
||||
{
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
long startRun = System.nanoTime();
|
||||
BufferUtil.writeTo(buffer.asReadOnlyBuffer(), out);
|
||||
long elapsedRun = System.nanoTime() - startRun;
|
||||
LOG.warn("run elapsed={}ms", elapsedRun / 1000);
|
||||
assertThat("Bytes in out equal bytes in buffer", Arrays.equals(bytes, out.toByteArray()), is(true));
|
||||
long start = System.nanoTime();
|
||||
for (int j = 0; j < iterations; j++)
|
||||
{
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
long startRun = System.nanoTime();
|
||||
BufferUtil.writeTo(buffer.asReadOnlyBuffer(), out);
|
||||
long elapsedRun = System.nanoTime() - startRun;
|
||||
// LOG.warn("run elapsed={}ms", elapsedRun / 1000);
|
||||
assertThat("Bytes in out equal bytes in buffer", Arrays.equals(bytes, out.toByteArray()), is(true));
|
||||
}
|
||||
long elapsed = System.nanoTime() - start;
|
||||
LOG.warn("elapsed={}ms average={}ms", elapsed / 1000, elapsed/iterations/1000);
|
||||
}
|
||||
long elapsed = System.nanoTime() - start;
|
||||
LOG.warn("elapsed={}ms average={}ms", elapsed / 1000, elapsed/iterations/1000);
|
||||
LOG.warn("overall average: {}ms", (System.nanoTime() - startTest) / testRuns / iterations / 1000);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue