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);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值