KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > demo > Performance2


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * -----------------
23  * Performance2.java
24  * -----------------
25  * (C) Copyright 2002, 2003, by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited).
28  * Contributor(s): -;
29  *
30  * $Id: Performance2.java,v 1.5 2003/06/13 15:46:07 mungady Exp $
31  *
32  * Changes (since 11-Oct-2002)
33  * ---------------------------
34  * 11-Oct-2002 : Fixed errors reported by Checkstyle (DG);
35  *
36  */

37 package org.jfree.chart.demo;
38
39 import java.util.Date JavaDoc;
40
41 /**
42  * A basic performance test for a couple of common operations.
43  *
44  * @author David Gilbert
45  */

46 public class Performance2 {
47
48     /** A double primitive. */
49     private double primitive = 42.0;
50
51     /** A number object. */
52     private Number JavaDoc object = new Double JavaDoc(42.0);
53
54     /**
55      * Default constructor.
56      */

57     public Performance2() {
58     }
59
60     /**
61      * Just use double value - should be fast.
62      *
63      * @return the double value.
64      */

65     public double getPrimitive() {
66         return primitive;
67     }
68
69     /**
70      * Creates a Number object every time the primitive is accessed - should be really slow.
71      *
72      * @return creates and returns a Number object.
73      */

74     public Number JavaDoc getPrimitiveAsObject() {
75         return new Double JavaDoc(primitive);
76     }
77
78     /**
79      * Returns the object - caller has to use doubleValue() method.
80      *
81      * @return an existing Number object.
82      */

83     public Number JavaDoc getObject() {
84         return object;
85     }
86
87     /**
88      * Returns a double value generated from the Object - should be similar to previous method,
89      * but is not!
90      *
91      * @return the doubleValue() for the Number.
92      */

93     public double getObjectAsPrimitive() {
94         return object.doubleValue();
95     }
96
97     /**
98      * Cycles through accessing the primitive.
99      *
100      * @param count the number of times to access.
101      */

102     public void getPrimitiveLoop(int count) {
103
104         double d = 0.0;
105         for (int i = 0; i < count; i++) {
106             d = getPrimitive();
107         }
108         System.out.println(d);
109
110     }
111
112     /**
113      * Cycles through accessing the primitive as an object.
114      *
115      * @param count the number of times to access.
116      */

117     public void getPrimitiveAsObjectLoop(int count) {
118
119         double d = 0.0;
120         for (int i = 0; i < count; i++) {
121             d = getPrimitiveAsObject().doubleValue();
122         }
123         System.out.println(d);
124
125     }
126
127     /**
128      * Cycles through accessing the object as a primitive.
129      *
130      * @param count the number of times to access.
131      */

132     public void getObjectAsPrimitiveLoop(int count) {
133
134         double d = 0.0;
135         for (int i = 0; i < count; i++) {
136             d = getObjectAsPrimitive();
137         }
138         System.out.println(d);
139
140     }
141
142     /**
143      * Cycles through accessing the object.
144      *
145      * @param count the number of times to access.
146      */

147     public void getObjectLoop(int count) {
148
149         double d = 0.0;
150         for (int i = 0; i < count; i++) {
151             d = getObject().doubleValue();
152         }
153         System.out.println(d);
154
155     }
156
157     /**
158      * Outputs the current status to the console.
159      *
160      * @param label the label.
161      * @param start the start time.
162      * @param end the end time.
163      */

164     public void status(String JavaDoc label, Date JavaDoc start, Date JavaDoc end) {
165         long elapsed = end.getTime() - start.getTime();
166         System.out.println(label + start.getTime() + "-->" + end.getTime() + " = " + elapsed);
167     }
168
169     /**
170      * The starting point for the performance test.
171      *
172      * @param args ignored.
173      */

174     public static void main(String JavaDoc[] args) {
175
176         Performance2 performance = new Performance2();
177         int count = 10000000;
178
179         for (int repeat = 0; repeat < 3; repeat++) { // repeat a few times just to make
180
// sure times are consistent
181
Date JavaDoc s1 = new Date JavaDoc();
182             performance.getPrimitiveLoop(count);
183             Date JavaDoc e1 = new Date JavaDoc();
184             performance.status("getPrimitive() : ", s1, e1);
185
186             Date JavaDoc s2 = new Date JavaDoc();
187             performance.getPrimitiveAsObjectLoop(count);
188             Date JavaDoc e2 = new Date JavaDoc();
189             performance.status("getPrimitiveAsObject() : ", s2, e2);
190
191             Date JavaDoc s3 = new Date JavaDoc();
192             performance.getObjectLoop(count);
193             Date JavaDoc e3 = new Date JavaDoc();
194             performance.status("getObject() : ", s3, e3);
195
196             Date JavaDoc s4 = new Date JavaDoc();
197             performance.getObjectAsPrimitiveLoop(count);
198             Date JavaDoc e4 = new Date JavaDoc();
199             performance.status("getObjectAsPrimitive() : ", s4, e4);
200
201             System.out.println("-------------------");
202         }
203     }
204
205 }
206
Popular Tags