FileDocCategorySizeDatePackage
BufferAllocBenchmark.javaAPI DocExample1496Sun Jan 20 17:07:28 GMT 2002com.ronsoft.books.nio.buffers

BufferAllocBenchmark

public class BufferAllocBenchmark extends Object

Fields Summary
Constructors Summary
Methods Summary
public static longdoAlloc(int size, int count, boolean direct)

		long start = System.currentTimeMillis();

		for (int i = 0; i < count; i++)  {
			ByteBuffer buf;

			if (direct) {
				buf = ByteBuffer.allocateDirect (size * 1024);
			} else {
				buf = ByteBuffer.allocate (size * 1024);
			}

		}

		return (System.currentTimeMillis() - start);
	
public static long[]doLoop(int count, boolean direct)

		long [] times = new long [count];

		for (int i = 0; i < times.length; i++) {
			times [i] = doAlloc (i * 8, 100, direct);
//			System.gc();
		}

		return (times);
	
public static voidmain(java.lang.String[] argv)

		long [] nonDirect;
		long [] direct;

		System.out.println ("Pre-running");
		doLoop (2, false);
		doLoop (2, true);

		System.gc();
		System.out.println ("allocating non-direct buffers");
		nonDirect = doLoop (25, false);

		System.gc();
		System.out.println ("allocating direct buffers");
		direct = doLoop (25, true);

		System.gc();
		System.out.println ("done");
		printResults (nonDirect, direct);
        
public static voidprintResults(long[] nonDirect, long[] direct)

		System.out.println ("Size\talloc\tallocDirect");

		for (int i = 0; i < nonDirect.length; i++) {
			System.out.println ("" + ((i + 1) * 8) + "\t"
				+ nonDirect [i] + "\t" + direct [i]);
		}