【ThinkingInJava】54、对List操作中本质的部分进行测试还有Queue中的操作测试性能比较
来源:互联网 发布:js解析xml文件的作用 编辑:程序博客网 时间:2024/06/01 20:51
<pre name="code" class="java"><pre name="code" class="java">package Lesson17Containers;//: containers/TestParam.java// A "data transfer object."public class TestParam { public final int size; public final int loops; public TestParam(int size, int loops) { this.size = size; this.loops = loops; } // Create an array of TestParam from a varargs sequence: public static TestParam[] array(int... values) { //吧values从一个可变参数改变为一个array数组 int size = values.length/2; TestParam[] result = new TestParam[size]; int n = 0; for(int i = 0; i < size; i++) result[i] = new TestParam(values[n++], values[n++]); return result; } // Convert a String array to a TestParam array: public static TestParam[] array(String[] values) { int[] vals = new int[values.length]; for(int i = 0; i < vals.length; i++) vals[i] = Integer.decode(values[i]); return array(vals); }} ///:~
package Lesson17Containers;//: containers/Tester.java// Applies Test objects to lists of different containers.import java.util.*;public class Tester<C> { public static int fieldWidth = 8; public static TestParam[] defaultParams= TestParam.array( 10, 5000, 100, 5000, 1000, 5000, 10000, 500); // Override this to modify pre-test initialization: protected C initialize(int size) { return container; } protected C container; private String headline = ""; private List<Test<C>> tests; private static String stringField() { return "%" + fieldWidth + "s"; } private static String numberField() { return "%" + fieldWidth + "d"; } private static int sizeWidth = 5; private static String sizeField = "%" + sizeWidth + "s"; private TestParam[] paramList = defaultParams; public Tester(C container, List<Test<C>> tests) { this.container = container; this.tests = tests; if(container != null) headline = container.getClass().getSimpleName(); } public Tester(C container, List<Test<C>> tests, TestParam[] paramList) { this(container, tests); this.paramList = paramList; } public void setHeadline(String newHeadline) { headline = newHeadline; } // Generic methods for convenience : public static <C> void run(C cntnr, List<Test<C>> tests){ new Tester<C>(cntnr, tests).timedTest(); } public static <C> void run(C cntnr, List<Test<C>> tests, TestParam[] paramList) { new Tester<C>(cntnr, tests, paramList).timedTest(); } private void displayHeader() { // Calculate width and pad with '-': int width = fieldWidth * tests.size() + sizeWidth; //8*tests.size()+5 int dashLength = width - headline.length() - 1; StringBuilder head = new StringBuilder(width); for(int i = 0; i < dashLength/2; i++) head.append('-'); head.append(' '); head.append(headline); head.append(' '); for(int i = 0; i < dashLength/2; i++) head.append('-'); System.out.println(head); // Print column headers: System.out.format(sizeField, "size"); for(Test test : tests) System.out.format(stringField(), test.name); System.out.println(); } // Run the tests for this container: public void timedTest() { displayHeader(); for(TestParam param : paramList) { System.out.format(sizeField, param.size); for(Test<C> test : tests) { C kontainer = initialize(param.size); long start = System.nanoTime(); // Call the overriden method: int reps = test.test(kontainer, param); long duration = System.nanoTime() - start; long timePerRep = duration / reps; // Nanoseconds System.out.format(numberField(), timePerRep); } System.out.println(); } }} ///:~
/*** 书本:《Thinking In Java》* 功能:对List操作中本质的部分进行测试还有Queue中的操作测试性能比较* 文件:ListPerformance.java* 时间:2015年5月4日14:27:20* 作者:cutter_point*/package Lesson17Containers;import java.util.*;import net.mindview.util.*;public class ListPerformance{static Random rand = new Random(); //产生随机数static int reps = 1000;static List<Test<List<Integer>>> tests = new ArrayList<Test<List<Integer>>>(); //用来测试liststatic List<Test<LinkedList<Integer>>> qTests = new ArrayList<Test<LinkedList<Integer>>>(); //LinkedListstatic{//测试list的addtests.add(new Test<List<Integer>>("add"){int test(List<Integer> list, TestParam tp){int loops = tp.loops; //要循环测试的次数int listSize = tp.size; //list容器长度for(int i = 0; i < loops; ++i){list.clear();for(int j = 0; j < listSize; ++j){list.add(j);}}return loops * listSize;}});//list的get函数tests.add(new Test<List<Integer>>("get"){int test(List<Integer> list, TestParam tp){int loops = tp.loops * reps; //要循环测试的次数int listSize = list.size(); //list容器长度for(int i = 0; i < loops; ++i){list.get(rand.nextInt(listSize));}return loops;}});//list的set函数tests.add(new Test<List<Integer>>("set"){int test(List<Integer> list, TestParam tp){int loops = tp.loops * reps; //要循环测试的次数int listSize = list.size(); //list容器长度for(int i = 0; i < loops; ++i){//吧47随机插入list.set(rand.nextInt(listSize), 47);}return loops;}});//list的set函数tests.add(new Test<List<Integer>>("iteradd"){int test(List<Integer> list, TestParam tp){final int LOOPS = 1000000;int half = list.size() / 2; //取长度的一般ListIterator<Integer> it = list.listIterator(half); //这个迭代器的起始位置是中间点for(int i = 0; i < LOOPS; ++i){it.add(47); //插入数据}return LOOPS;}});//list的insert函数tests.add(new Test<List<Integer>>("insert"){int test(List<Integer> list, TestParam tp){int loops = tp.loops;for(int i = 0; i < loops; ++i){list.add(5, 47); //在指定的位置不断地插入数据}return loops;}});//list的remove函数tests.add(new Test<List<Integer>>("remove"){int test(List<Integer> list, TestParam tp){int loops = tp.loops;int size = tp.size;for(int i = 0; i < loops; ++i){list.clear();list.addAll(new CountingIntegerList(size)); //添加数据0到size(不包含size)while(list.size() > 5){list.remove(5);}}return loops * size;}});/***********************************************************************************************************************************///linkedlist的addFirst函数qTests.add(new Test<LinkedList<Integer>>("addFirst"){int test(LinkedList<Integer> list, TestParam tp){int loops = tp.loops; //要循环测试的次数int listSize = tp.size; //list容器长度for(int i = 0; i < loops; ++i){list.clear();for(int j = 0; j < listSize; ++j){list.addFirst(47);}}return loops * listSize;}});//linkedlist的addLast函数qTests.add(new Test<LinkedList<Integer>>("addLast"){int test(LinkedList<Integer> list, TestParam tp){int loops = tp.loops; //要循环测试的次数int listSize = tp.size; //list容器长度for(int i = 0; i < loops; ++i){list.clear();for(int j = 0; j < listSize; ++j){list.addLast(47);}}return loops * listSize;}});//linkedlist的rmFirst函数qTests.add(new Test<LinkedList<Integer>>("rmFirst"){int test(LinkedList<Integer> list, TestParam tp){int loops = tp.loops; //要循环测试的次数int size = tp.size; //list容器长度for(int i = 0; i < loops; ++i){list.clear();list.addAll(new CountingIntegerList(size));while(list.size() > 0)list.removeFirst(); //去掉第一个元素}return loops * size;}});//linkedlist的rmLast函数qTests.add(new Test<LinkedList<Integer>>("rmLast"){int test(LinkedList<Integer> list, TestParam tp){int loops = tp.loops; //要循环测试的次数int size = tp.size; //list容器长度for(int i = 0; i < loops; ++i){list.clear();list.addAll(new CountingIntegerList(size));while(list.size() > 0)list.removeLast(); //去掉第一个元素}return loops * size;}});}static class ListTester extends Tester<List<Integer>>{public ListTester(List<Integer> container,List<Test<List<Integer>>> tests){super(container, tests);}@Overrideprotected List<Integer> initialize(int size){this.container.clear();//清楚所有的数据元素this.container.addAll(new CountingIntegerList(size)); //得到0到size的数据return this.container;}public static void run(List<Integer> list, List<Test<List<Integer>>> tests){new ListTester(list, tests).timedTest();//显示信息}}public static void main(String[] args){Tester<List<Integer>> arrayTest = new Tester<List<Integer>>(null, tests.subList(1, 3)){@Overrideprotected List<Integer> initialize(int size){Integer[] ia = Generated.array(Integer.class, new CountingGenerator.Integer(), size);return Arrays.asList(ia);}};arrayTest.setHeadline("Array as list");arrayTest.timedTest();Tester.defaultParams= TestParam.array( 10, 5000, 100, 5000, 1000, 1000, 10000, 200);ListTester.run(new ArrayList<Integer>(), tests); ListTester.run(new LinkedList<Integer>(), tests); ListTester.run(new Vector<Integer>(), tests); Tester.fieldWidth = 12; Tester<LinkedList<Integer>> qTest = new Tester<LinkedList<Integer>>( new LinkedList<Integer>(), qTests); qTest.setHeadline("Queue tests"); qTest.timedTest();}}
输出:
--- Array as list ---
size get set
10 13 15
100 12 14
1000 11 12
10000 11 14
--------------------- ArrayList ---------------------
size add get set iteradd insert remove
10 77 13 15 27 275 201
100 5 12 14 13 269 18
1000 20 12 19 51 212 61
10000 12 13 15 417 1436 508
--------------------- LinkedList ---------------------
size add get set iteradd insert remove
10 103 28 28 41 182 174
100 10 42 43 14 92 39
1000 12 392 391 9 9 22
10000 27 4406 4418 68 32 21
----------------------- Vector -----------------------
size add get set iteradd insert remove
10 115 15 17 28 265 104
100 8 15 17 19 268 25
1000 11 15 16 55 172 80
10000 11 15 16 428 1381 513
-------------------- Queue tests --------------------
size addFirst addLast rmFirst rmLast
10 79 64 91 105
100 8 7 11 11
1000 26 35 54 44
10000 8 7 18 19
- 【ThinkingInJava】54、对List操作中本质的部分进行测试还有Queue中的操作测试性能比较
- 【ThinkingInJava】55、对set中的操作测试性能比较
- 对部分整型(char)操作的测试
- 对链表进行操作测试
- LoadRunner中对接口进行性能测试的常用方法
- LoadRunner中对接口进行性能测试的常用方法
- JAVA中对LIST进行排序操作
- 软件性能测试的本质
- 软件性能测试的本质
- 软件性能测试的本质
- 软件性能测试的本质
- 软件性能测试的本质
- 性能测试操作流程
- JS中对时间的基本操作测试
- groovy中对文件,集合,测试,数据库的操作例子
- 在eclipse中利用spring-osgi中的测试框架对osgi 的 boundle进行测试
- python对list中的每个元素进行某种操作
- 对进程的常见操作测试
- Android系统基本框架结构[续]
- [POJ 1195] Mobile phones · 树状数组
- Redis中key-value实现
- C++刷题——2713: 函数---求x的y次方
- 递归函数详解
- 【ThinkingInJava】54、对List操作中本质的部分进行测试还有Queue中的操作测试性能比较
- Flipping Parentheses
- 猫猫学iOS(四十九)多线程网络之线程的创建NSThreand
- 破解office13VOL
- Web端服务器推送技术原理分析及dwr框架简单的使用
- 【ThinkingInJava】55、对set中的操作测试性能比较
- 【ThinkingInJava】56、查询出文件路径下所有的文件
- 带自定义title的tabhost
- 【ThinkingInJava】57、批量修改文件的名字