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);
}
}