KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > QueueSpeedTest


1 // $Id: QueueSpeedTest.java,v 1.5 2004/07/05 14:15:11 belaban Exp $
2

3
4 package org.jgroups.tests;
5
6 import org.jgroups.util.LinkedListQueue;
7 import org.jgroups.util.Queue;
8
9
10 /**
11  * Measures the speed of inserting and removing 1 million elements into/from a Queue and a LinkedQueue.
12  *
13  * @author Bela Ban
14  */

15 public class QueueSpeedTest {
16     int num_elements=1000000;
17     final int NUM=10;
18
19
20     public QueueSpeedTest(int num_elements) {
21         this.num_elements=num_elements;
22     }
23
24
25     public void start() throws Exception JavaDoc {
26         double q1=0, q2=0, diff;
27
28
29         System.out.println("warming up cache");
30         measureQueue();
31         measureLinkedListQueue();
32
33         System.out.println("running insertions " + NUM + " times (will take average)");
34         for(int i=0; i < NUM; i++) {
35             System.out.println("Round #" + (i + 1));
36             q1+=measureQueue();
37             q2+=measureLinkedListQueue();
38         }
39
40         q1=q1 / NUM;
41         q2=q2 / NUM;
42
43         System.out.println("Time to insert and remove " + num_elements + " into Queue: " + q1 + " ms");
44         System.out.println("Time to insert and remove " + num_elements + " into LinkedListQueue: " + q2 + " ms");
45
46         diff=(long)(q2 - q1);
47         System.out.println("diff is " + Math.abs(diff) + "; " + (q2 < q1 ? "LinkedListQueue" : "Queue") + " is faster");
48     }
49
50
51     long measureQueue() throws Exception JavaDoc {
52         Queue q=new Queue();
53         long start, stop;
54
55         start=System.currentTimeMillis();
56         for(int i=0; i < num_elements; i++) {
57             if(i % 2 == 0)
58                 q.add(new Integer JavaDoc(i));
59             else
60                 q.addAtHead(new Integer JavaDoc(i));
61         }
62
63         while(q.size() > 0)
64             q.remove();
65
66         stop=System.currentTimeMillis();
67         return stop - start;
68     }
69
70
71     long measureLinkedListQueue() throws Exception JavaDoc {
72         LinkedListQueue q=new LinkedListQueue();
73         long start, stop;
74
75         start=System.currentTimeMillis();
76         for(int i=0; i < num_elements; i++) {
77             if(i % 2 == 0)
78                 q.add(new Integer JavaDoc(i));
79             else
80                 q.addAtHead(new Integer JavaDoc(i));
81         }
82
83         while(q.size() > 0)
84             q.remove();
85
86         stop=System.currentTimeMillis();
87         return stop - start;
88     }
89
90
91     public static void main(String JavaDoc[] args) {
92         int num_elements=1000000;
93
94         for(int i=0; i < args.length; i++) {
95             if("-num_elements".equals(args[i])) {
96                 num_elements=Integer.parseInt(args[++i]);
97                 continue;
98             }
99             help();
100             return;
101         }
102
103         try {
104             new QueueSpeedTest(num_elements).start();
105         }
106         catch(Exception JavaDoc ex) {
107             System.err.println(ex);
108         }
109     }
110
111
112     static void help() {
113         System.out.println("QueueSpeedTest [-help] [-num_elements <num>]");
114     }
115 }
116
117
Popular Tags