import java.nio.*;
import java.util.*;
class ByteBufferCopyTest
{
public static void main(String aaahhhrgs[])
{
verify(5);
for (int size=1000000; size<=10000000; size*=10)
{
test(size);
}
}
private static byte[] create(int size)
{
byte b[] = new byte[size];
for (int i=0; i<size; i++)
{
b[i] = (byte)('A'+(int)(Math.random() * 27));
}
return b;
}
private static void verify(int size)
{
byte a[] = create(size);
byte b[] = create(size);
byte c0[] = combineAC(a, b);
byte c1[] = combineBB(a, b);
System.out.println("a = "+Arrays.toString(a));
System.out.println("b = "+Arrays.toString(b));
System.out.println("c0 = "+Arrays.toString(c0));
System.out.println("c1 = "+Arrays.toString(c1));
}
private static void test(int size)
{
byte a[] = create(size);
byte b[] = create(size);
for (int runs=10; runs<=50; runs+=10)
{
testAC(a,b,runs);
testBB(a,b,runs);
}
}
private static void testAC(byte a[], byte b[], int runs)
{
long before = System.currentTimeMillis();
int result = 0;
for (int i=0; i<runs; i++)
{
result += combineAC(a,b).length;
}
long after = System.currentTimeMillis();
System.out.println(String.format("%10d",result)+" Time for "+runs+" runs on arrays of size "+a.length+" with AC is "+(after-before));
}
private static void testBB(byte a[], byte b[], int runs)
{
long before = System.currentTimeMillis();
int result = 0;
for (int i=0; i<runs; i++)
{
result += combineBB(a,b).length;
}
long after = System.currentTimeMillis();
System.out.println(String.format("%10d",result)+" Time for "+runs+" runs on arrays of size "+a.length+" with BB is "+(after-before));
}
private static byte[] combineAC(byte a[], byte[] b)
{
byte result[] = new byte[a.length+b.length];
System.arraycopy(a,0,result,0,a.length);
System.arraycopy(b,0,result,a.length,b.length);
return result;
}
private static byte[] combineBB(byte a[], byte[] b)
{
byte result[] = new byte[a.length+b.length];
((ByteBuffer)ByteBuffer.allocateDirect(a.length + b.length).put(a).put(b).rewind()).get(result);
return result;
}
}