bufferallocbenchmark.java

来自「java nio 编程一个实例子.服务端程序」· Java 代码 · 共 74 行

JAVA
74
字号
package com.ronsoft.books.nio.buffers;import java.nio.*;import java.nio.channels.*;import java.io.IOException;public class BufferAllocBenchmark{	public static long doAlloc (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 void printResults (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]);		}	}	public static void main (String [] argv)		throws IOException	{		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);        }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?