zoukankan      html  css  js  c++  java
  • LinkedList ArrayList测试2

    下面测试他们的性能 如何 

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.ListIterator;
    
    
    
    
    public class ListTest {
    	private static final int REPS =100;
    	
    	private abstract static class Tester {
    		String name ;
    		int size ;
    		Tester(String name , int size ){
    			this.name= name ;
    			this.size = size ;
    		}
    		
    		abstract void test(List a ) ;
    	}
    	//测试的一个数组
    	private static Tester [] tests ={
    		new Tester("get" , 300) {
    			
    			void test(List a) {
    				for (int i =0; i<REPS ; i++){
    					for (int j =0; j< a.size(); j++){
    						a.get(j) ;
    					}
    				}
    			}
    		},
    		new Tester("iteration",300) {
    			void test(List a) {
    				for (int i =0; i<REPS ; i++){
    					Iterator it = a.iterator() ;
    					while (it.hasNext()) {
    						it.next() ;
    					}
    				}
    			}
    		},
    		new Tester("insert" , 1000) {
    			
    			void test(List a) {
    				int half = a.size()/2;
    				String s = "test" ;
    				ListIterator it = a.listIterator() ;
    				for (int i =0; i<size*10; i++){
    					it.add(s) ;
    				}
    			}
    		},
    		new Tester("remove", 5000) {
    			
    			void test(List a) {
    				ListIterator it = a.listIterator(3) ;
    				while (it.hasNext()) {
    					it.next() ;
    					it.remove() ;
    				}
    			}
    		},
    	} ;
    	
    	public static void test (List a ){
    		System.out.println("testing"+ a.getClass().getName()) ; 
    		for (int i =0;i< tests.length; i++){
    			//collections中的fill方法:使用指定元素替换指定列表中的所有元素
    			Collections.fill(a, tests[i]) ;
    			System.out.print(tests[i].name) ;
    			long t1 = System.currentTimeMillis() ;
    			tests[i].test(a);
    			long t2 = System.currentTimeMillis() ;
    			System.out.println(":"+ (t2-t1));
    		}
    	}
    	
    	public static void main (String [] args ){
    		test(new ArrayList ()) ;
    		test(new LinkedList()) ;
    	}
    	
    	
    	
    }
    

      

  • 相关阅读:
    POI_Excel表格数据导入导出实例--支持xls/xlsx格式
    js图片压缩工具---base64码上传插件,兼容h5和微信端(lrz.mobile.min.js)
    同一个页面,加载不同版本jQuery
    This method isn't transactional
    jquery.cookie的使用,记住用户名
    正则表达式 2017/6/12
    kSet 2017/6/6
    差分与二维差分
    求组合数
    高精度
  • 原文地址:https://www.cnblogs.com/chuiyuan/p/4338941.html
Copyright © 2011-2022 走看看