同步处理的ArrayList和Vector性能小测试

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;


public class ArrayTest {
	private List<Integer> mArrayList;
	private Vector<Integer> mVector;
	private ArrayTest(){
//		mArrayList =  Collections.synchronizedList(new ArrayList<String> ());
		mArrayList = new ArrayList<Integer>();
		mVector = new Vector<Integer>();
	}
	private synchronized void addList(){
		int i = 0;
		long oldTimes = System.nanoTime();
		while(i <= 99){
			mArrayList.add(100);
			i++;
		}
		long nowTimes = System.nanoTime();
		System.out.println("add "+(nowTimes-oldTimes));
		
	}
	private synchronized void getList(){
		long oldTimes = System.nanoTime();
		Iterator<Integer> iterator = mArrayList.iterator();
		while(iterator.hasNext()){
			iterator.next();
			
		}
		long nowTimes = System.nanoTime();
		System.out.println("get "+(nowTimes-oldTimes));
	}
	
	private synchronized void remove(){
		int i = 0;
		long oldTimes = System.nanoTime();
		while(i <= 99){
			mArrayList.removeAll(mArrayList);
			i++;
		}
		long nowTimes = System.nanoTime();
		System.out.println("remove "+(nowTimes-oldTimes));
	}
	private synchronized void remove1(){
		int i = 0;
		long oldTimes = System.nanoTime();
		while(i <= 99){
			mVector.removeAllElements();
			i++;
		}
		long nowTimes = System.nanoTime();
		System.out.println("mVector remove "+(nowTimes-oldTimes));
	}
	private void addList1(){
		int i = 0;
		long oldTimes = System.nanoTime();
		while(i <= 99){
			mVector.add(100);
			i++;
		}
		long nowTimes = System.nanoTime();
		System.out.println("mVector add "+(nowTimes-oldTimes));
		
	}
	private void getList1(){
		long oldTimes = System.nanoTime();
		Iterator<Integer> iterator =  mVector.iterator();
		while(iterator.hasNext()){
			iterator.next();
		}
		long nowTimes = System.nanoTime();
		System.out.println("mVector get "+(nowTimes-oldTimes));
	}
	
	public static void main(String[] args) {
		ArrayTest arrayTest = new ArrayTest();
		arrayTest.addList();
		arrayTest.getList();
		arrayTest.remove();
		arrayTest.addList1();
		arrayTest.getList1();
		arrayTest.remove1();
	}
}

add 799077
get 337551
remove 58513
mVector add 48640
mVector get 279402

mVector remove 14995


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值