java 代码时间和内存测试_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java 代码时间和内存测试

java 代码时间和内存测试

 2015/4/19 0:26:46  flyvszhb  程序员俱乐部  我要评论(0)
  • 摘要://测试用了多少内存importjava.util.ArrayList;importjava.util.List;publicclassMemoryTest{publicstaticlongused(){longtotal=Runtime.getRuntime().totalMemory();longfree=Runtime.getRuntime().freeMemory();return(total-free);}publicstaticvoidmain(String[]args)
  • 标签:测试 Java 代码 内存
class="java">
// 测试用了多少内存
import java.util.ArrayList;
import java.util.List;

public class MemoryTest {
	public static long used() {
		long total = Runtime.getRuntime().totalMemory();
		long free = Runtime.getRuntime().freeMemory();
		return (total - free);
	}

	public static void main(String[] args) {
		long start = used();
        List<String> list = new ArrayList<String>();
        int total = 10000;
        while (total > 0) {
        	total--;
			list.add(new String());
		}
        long end = used();
        System.out.println("memory increased by "+(end-start)+"bytes");
	}
}


// 测试执行时间,这个大家都知道
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class TimeTest {
    
	public static void main(String[] args) {
		List<String> arrayList = new ArrayList<String>();
		int total1 = 1000000;
		long start = System.currentTimeMillis();
		while (total1 > 0) {
			total1--;
			arrayList.add("testArrayList");
		}
		long end = System.currentTimeMillis();
		System.out.println("arrayList took " + (end - start) + " Millisecond");
		
		List<String> linkedList = new LinkedList<String>();
		int total2 = 1000000;
		long start2 = System.currentTimeMillis();
		while (total2 > 0) {
			total2--;
			linkedList.add("testLinkedList");
		}
		long end2 = System.currentTimeMillis();
		System.out.println("linkendList took " + (end2 - start2) + " Millisecond");
	}
}



// 运用aop计算程序执行时间
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class TimeTest {
	public static void main(String[] args) {
		try {
			Foo foo = (Foo) Handler.newInstance(new FooImpl());
			foo.testArrayList();
			foo.testLinkedList();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}

class Handler implements InvocationHandler {
	
    Object obj;
	
    private Handler(Object object) {
		this.obj = object;
	}
    
	public static Object newInstance(Object obj) {
		return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj
				.getClass().getInterfaces(), new Handler(obj));
	}
    
	@Override
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		Object result;
		try {
			System.out.print("begin method :" + method.getName() + "(");
			for (int i = 0; args != null && i < args.length; i++) {
				if (i > 0) {
					System.out.print(",");
				}
				System.out.print(" " + args[i].toString());
			}
			System.out.println(" )");
			long start = System.currentTimeMillis();
			result = method.invoke(obj, args);
			long end = System.currentTimeMillis();
			System.out.println("the method " + method.getName() + " lasts "
					+ (end - start) + "ms");
		} catch (InvocationTargetException e) {
			throw e.getTargetException();
		} catch (Exception e) {
			throw new RuntimeException("unexpected invocation exception: "
					+ e.getMessage());
		} finally {
			System.out.println("end method :" + method.getName());
		}
		return result;
	}
}

interface Foo {
	public void testArrayList();

	public void testLinkedList();
}

class FooImpl implements Foo {
	private List link = new LinkedList();
	private List array = new ArrayList();

	public FooImpl() {
		for (int i = 0; i < 10000; i++) {
			array.add(new Integer(i));
			link.add(new Integer(i));
		}
	}

	public void testArrayList() {
		for (int i = 0; i < 10000; i++)
			array.get(i);
	}

	public void testLinkedList() {
		for (int i = 0; i < 10000; i++)
			link.get(i);
	}
}

发表评论
用户名: 匿名