友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!阅读过程发现任何错误请告诉我们,谢谢!! 报告错误
狗狗书籍 返回本书目录 我的书架 我的书签 TXT全本下载 进入书吧 加入书签

Java编程思想第4版[中文版](PDF格式)-第115章

按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!




    }  

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

        }  

      }  

    };  



                                                                                     247 


…………………………………………………………Page 249……………………………………………………………

    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(half);  

        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) {  

    // A trick to print out the class name:  

    System。out。println(〃Testing 〃 +   

      a。getClass()。getName());  

    for(int i = 0; i 《 tests。length; i++) {  

      Collection1。fill(a; tests'i'。size);  

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

  }  

} ///:~  

  

内部类Tester 是一个抽象类,用于为特定的测试提供一个基础类。它包含了一个要在测试开始时打印的字 

串、一个用于计算测试次数或元素数量的size 参数、用于初始化字段的一个构建器以及一个抽象方法 

test()。test()做的是最实际的测试工作。各种类型的测试都集中到一个地方:tests 数组。我们用继承于 

Tester 的不同匿名内部类来初始化该数组。为添加或删除一个测试项目,只需在数组里简单地添加或移去一 

个内部类定义即可,其他所有工作都是自动进行的。  



                                                                                          248 


…………………………………………………………Page 250……………………………………………………………

首先用元素填充传递给 test()的List,然后对tests 数组中的测试计时。由于测试用机器的不同,结果当然 

也会有所区别。这个程序的宗旨是揭示出不同集合类型的相对性能比较。下面是某一次运行得到的结果:  

  

  



Type       Get  Iteration Insert Remove  



ArrayList 110  490        3790   8730  



LinkedList 1980 220       110    110  



  

可以看出,在ArrayList 中进行随机访问(即get())以及循环反复是最划得来的;但对于LinkedList 却是 

一个不小的开销。但另一方面,在列表中部进行插入和删除操作对于 LinkedList 来说却比ArrayList 划算得 

多。我们最好的做法也许是先选择一个ArrayList 作为自己的默认起点。以后若发现由于大量的插入和删除 

造成了性能的降低,再考虑换成LinkedList 不迟。  

  

2。 决定使用何种 Set  

可在ArraySet 以及HashSet 间作出选择,具体取决于Set 的大小(如果需要从一个Set 中获得一个顺序列 

表,请用TreeSet;注释⑧)。下面这个测试程序将有助于大家作出这方面的抉择:  

  

//: SetPerformance。java  

package c08。newcollections;  

import java。util。*;  

  

public class SetPerformance {  

  private static final int REPS = 200;  

  private abstract static class Tester {  

    String name;  

    Tester(String name) { this。name = name; }  

    abstract void test(Set s; int size);  

  }  

  private static Tester'' tests = {  

    new Tester(〃add〃) {   

      void test(Set s; int size) {  

        for (int i = 0; i 《 REPS; i++) {  

          s。clear();  

          Collection1。fill(s; size);  

        }  

      }  

    };  

    new Tester(〃contains〃) {   

      void test(Set s; int size) {  

        for(int i = 0; i 《 REPS; i++)  

          for(int j = 0; j 《 size; j++)  

            s。contains(Integer。toString(j));  

      }  

    };  

    new Tester(〃iteration〃) {   

      void test(Set s; int size) {  

        for(int i = 0; i 《 REPS * 10; i++) {  

          Iterator it = s。iterator();  

          while(it。hasNext())  

            it。next();  

        }  



                                                                                            249 


…………………………………………………………Page 251……………………………………………………………

      }  

    };  

  };  

  public static void test(Set s; int size) {  

    // A trick to print out the class name:  

    System。out。println(〃Testing 〃 +   

      s。getClass()。getName() + 〃 size 〃 + size);  

    Collection1。fill(s; size);  

    for(int i = 0; i 《 tests。length; i++) {  

      System。out。print(tests'i'。name);  

      long t1 = System。currentTimeMillis();  

      tests'i'。test(s; size);  

      long t2 = System。currentTimeMillis();  

      System。out。println(〃: 〃 +   

        ((double)(t2 t1)/(double)size));  

    }  

  }  

  public static void main(String'' args) {  

    // Small:  

    test(new TreeSet(); 10);  

    test(new HashSet(); 10);  

    // Medium:  

    test(new TreeSet(); 100);  

    test(new HashSet(); 100);  

    // Large:  

    test(new HashSet(); 1000);  

    test(new TreeSet(); 1000);  

  }  

} ///:~  

  

⑧:TreeSet 在本书写作时尚未成为一个正式的特性,但在这个例子中可以很轻松地为其添加一个测试。  

  

最后对ArraySet 的测试只有500 个元素,而不是 1000 个,因为它太慢了。  

  

类型 测试大小 添加 包含 反复  

  

  



Type    Test size Add  Contains Iteration  



        10        22。0 11。0     16。0  



TreeSet 100       22。5 13。2     12。1  



        1000      31。1 18。7     11。8  



        10        5。0  6。0      27。0  



HashSet 100       6。6  6。6      10。9  



        1000      7。4  6。6      9。5  



  

  

进行ad
返回目录 上一页 下一页 回到顶部 0 0
未阅读完?加入书签已便下次继续阅读!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!