KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > PerfText


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2005 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution;
10
11 import javolution.context.PoolContext;
12 import javolution.lang.MathLib;
13 import javolution.text.Text;
14 import javolution.text.TextBuilder;
15 import javolution.text.TypeFormat;
16
17 /**
18  * <p> This class holds {@link javolution.lang} benchmark.</p>
19  *
20  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
21  * @version 3.0, February 20, 2005
22  */

23 final class PerfText extends Javolution implements Runnable JavaDoc {
24
25     static volatile int COUNT = 1000; // Volatile to avoid loop unrolling.
26

27     private final String JavaDoc STRING = "Concatenates this line 1000 times (resulting in a text of about 80,000 characters)";
28
29     private final Text TEXT = Text.valueOf(STRING);
30
31     private final Text ONE_CHAR = Text.valueOf('X');
32     
33     /**
34      * Executes benchmark.
35      */

36     public void run() {
37         println("//////////////////////////////");
38         println("// Package: javolution.text //");
39         println("//////////////////////////////");
40         println("");
41
42         println("-- Primitive types formatting --");
43         println("");
44         int n = 100000;
45         int ii = MathLib.random(Integer.MIN_VALUE, Integer.MAX_VALUE);
46         long ll = MathLib.random(Long.MIN_VALUE, Long.MAX_VALUE);
47
48         print("StringBuffer.append(int): ");
49         for (int j=0; j < 10; j++) {
50             StringBuffer JavaDoc tmp = new StringBuffer JavaDoc();
51             startTime();
52             for (int i = 0; i < n; i++) {
53                 tmp.setLength(0);
54                 tmp.append(ii);
55             }
56             keepBestTime(n);
57         }
58         println(endTime());
59         
60         print("TextBuilder.append(int): ");
61         for (int j=0; j < 10; j++) {
62             TextBuilder tmp = new TextBuilder();
63             startTime();
64             for (int i = 0; i < n; i++) {
65                 tmp.clear().append(ii);
66             }
67             keepBestTime(n);
68             if (!tmp.contentEquals(ii + "")) throw new Error JavaDoc();
69         }
70         println(endTime());
71         
72         print("StringBuffer.append(long): ");
73         for (int j=0; j < 10; j++) {
74             StringBuffer JavaDoc tmp = new StringBuffer JavaDoc();
75             startTime();
76             for (int i = 0; i < n; i++) {
77                 tmp.setLength(0);
78                 tmp.append(ll);
79             }
80             keepBestTime(n);
81         }
82         println(endTime());
83         
84         print("TextBuilder.append(long): ");
85         for (int j=0; j < 10; j++) {
86             TextBuilder tmp = new TextBuilder();
87             startTime();
88             for (int i = 0; i < n; i++) {
89                 tmp.clear().append(ll);
90             }
91             keepBestTime(n);
92             if (!tmp.contentEquals(ll + "")) throw new Error JavaDoc();
93         }
94         println(endTime());
95         
96         /*@JVM-1.1+@
97         float ff = MathLib.random(-1.0f, 1.0f);
98         double dd = MathLib.random(-1.0d, 1.0d);
99         
100         print("StringBuffer.append(float): ");
101         for (int j=0; j < 10; j++) {
102             StringBuffer tmp = new StringBuffer();
103             startTime();
104             for (int i = 0; i < n; i++) {
105                 tmp.setLength(0);
106                 tmp.append(ff);
107             }
108             keepBestTime(n);
109         }
110         println(endTime());
111         
112         print("TextBuilder.append(float): ");
113         for (int j=0; j < 10; j++) {
114             TextBuilder tmp = new TextBuilder();
115             startTime();
116             for (int i = 0; i < n; i++) {
117                 tmp.clear().append(ff);
118             }
119             keepBestTime(n);
120             if (ff != Float.parseFloat(tmp.toString())) throw new Error();
121         }
122         println(endTime());
123         
124         print("StringBuffer.append(double): ");
125         for (int j=0; j < 10; j++) {
126             StringBuffer tmp = new StringBuffer();
127             startTime();
128             for (int i = 0; i < n; i++) {
129                 tmp.setLength(0);
130                 tmp.append(dd);
131             }
132             keepBestTime(n);
133         }
134         println(endTime());
135         
136         print("TextBuilder.append(double): ");
137         for (int j=0; j < 10; j++) {
138             TextBuilder tmp = new TextBuilder();
139             startTime();
140             for (int i = 0; i < n; i++) {
141                 tmp.clear().append(dd);
142             }
143             keepBestTime(n);
144             if (dd != Double.parseDouble(tmp.toString())) throw new Error();
145         }
146         println(endTime());
147         /**/

148         println("");
149         
150         println("-- Primitive types parsing --");
151         println("");
152         String JavaDoc is = "" + ii;
153         String JavaDoc ls = "" + ll;
154
155         print("Integer.parseInt(String): ");
156         for (int j=0; j < 10; j++) {
157             startTime();
158             for (int i = 0; i < n; i++) {
159                 if (Integer.parseInt(is) != ii) throw new Error JavaDoc();
160             }
161             keepBestTime(n);
162         }
163         println(endTime());
164         
165         print("TypeFormat.parseInt(String): ");
166         for (int j=0; j < 10; j++) {
167             startTime();
168             for (int i = 0; i < n; i++) {
169                 if (TypeFormat.parseInt(is) != ii) throw new Error JavaDoc();
170             }
171             keepBestTime(n);
172         }
173         println(endTime());
174         
175         print("Long.parseLong(String): ");
176         for (int j=0; j < 10; j++) {
177             startTime();
178             for (int i = 0; i < n; i++) {
179                 if (Long.parseLong(ls) != ll) throw new Error JavaDoc();
180             }
181             keepBestTime(n);
182         }
183         println(endTime());
184         
185         print("TypeFormat.parseLong(String): ");
186         for (int j=0; j < 10; j++) {
187             startTime();
188             for (int i = 0; i < n; i++) {
189                 if (TypeFormat.parseLong(ls) != ll) throw new Error JavaDoc();
190             }
191             keepBestTime(n);
192         }
193         println(endTime());
194         
195         /*@JVM-1.1+@
196         String fs = "" + ff;
197         String ds = "" + dd;
198
199         print("Float.parseFloat(String): ");
200         for (int j=0; j < 10; j++) {
201             startTime();
202             for (int i = 0; i < n; i++) {
203                 if (Float.parseFloat(fs) != ff) throw new Error();
204             }
205             keepBestTime(n);
206         }
207         println(endTime());
208         
209         print("TypeFormat.parseFloat(String): ");
210         for (int j=0; j < 10; j++) {
211             startTime();
212             for (int i = 0; i < n; i++) {
213                 if (TypeFormat.parseFloat(fs) != ff) throw new Error();
214             }
215             keepBestTime(n);
216         }
217         println(endTime());
218         
219         print("Double.parseDouble(String): ");
220         for (int j=0; j < 10; j++) {
221             startTime();
222             for (int i = 0; i < n; i++) {
223                 if (Double.parseDouble(ds) != dd) throw new Error();
224             }
225             keepBestTime(n);
226         }
227         println(endTime());
228         
229         print("TypeFormat.parseDouble(String): ");
230         for (int j=0; j < 10; j++) {
231             startTime();
232             for (int i = 0; i < n; i++) {
233                 if (TypeFormat.parseDouble(ds) != dd) throw new Error();
234             }
235             keepBestTime(n);
236         }
237         println(endTime());
238         /**/

239         println("");
240         
241         println("-- String/StringBuffer versus Text --");
242         println("");
243
244         println("\"" + STRING + "\"");
245
246         print("String \"+\" operator: ");
247         String JavaDoc str = "";
248         for (int i = 0; i < 2; i++) {
249             str = "";
250             startTime();
251             for (int j = COUNT; --j >= 0;) {
252                 str += STRING;
253             }
254             keepBestTime(COUNT);
255         }
256         println(endTime());
257
258         print("StringBuffer \"append\" : ");
259         for (int i = 0; i < 100; i++) {
260             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
261             startTime();
262             for (int j = COUNT; --j >= 0;) {
263                 sb.append(STRING);
264             }
265             keepBestTime(COUNT);
266         }
267         println(endTime());
268
269         print("Text \"concat\" (heap): ");
270         for (int i = 0; i < 100; i++) {
271             Text txt = Text.EMPTY;
272             startTime();
273             for (int j = COUNT; --j >= 0;) {
274                 txt = txt.concat(TEXT);
275             }
276             keepBestTime(COUNT);
277         }
278         println(endTime());
279
280         print("Text \"concat\" (stack): ");
281         for (int i = 0; i < 100; i++) {
282             PoolContext.enter();
283             Text txt = Text.EMPTY;
284             startTime();
285             for (int j = COUNT; --j >= 0;) {
286                 txt = txt.concat(TEXT);
287             }
288             keepBestTime(COUNT);
289             PoolContext.exit();
290         }
291         println(endTime());
292
293         println("");
294         println("Inserts one character at random locations 1,000 times to the 80,000 characters text.");
295
296         print("StringBuffer insert: ");
297         for (int i = 0; i < 100; i++) {
298             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(str);
299             startTime();
300             for (int j = COUNT; --j >= 0;) {
301                 int index = MathLib.random(0, sb.length());
302                 sb.insert(index, 'X');
303             }
304             keepBestTime(COUNT);
305         }
306         println(endTime());
307
308         print("Text insert (heap): ");
309         for (int i = 0; i < 100; i++) {
310             Text txt = Text.valueOf(str);
311             startTime();
312             for (int j = COUNT; --j >= 0;) {
313                 int index = MathLib.random(0, txt.length());
314                 txt = txt.insert(index, ONE_CHAR);
315             }
316             keepBestTime(COUNT);
317         }
318         println(endTime());
319
320         print("Text insert (stack): ");
321         for (int i = 0; i < 100; i++) {
322             PoolContext.enter();
323             Text txt = Text.valueOf(str);
324             startTime();
325             for (int j = COUNT; --j >= 0;) {
326                 int index = MathLib.random(0, txt.length());
327                 txt = txt.insert(index, ONE_CHAR);
328             }
329             keepBestTime(COUNT);
330             PoolContext.exit();
331         }
332         println(endTime());
333
334         println("");
335         println("Delete 1,000 times one character at random location from the 80,000 characters text.");
336
337         print("StringBuffer delete: ");
338         for (int i = 0; i < 100; i++) {
339             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(str);
340             startTime();
341             for (int j = COUNT; --j >= 0;) {
342                 int index = MathLib.random(0, sb.length() - 1);
343                 sb.deleteCharAt(index);
344             }
345             keepBestTime(COUNT);
346         }
347         println(endTime());
348
349         print("Text delete (heap): ");
350         for (int i = 0; i < 100; i++) {
351             Text txt = Text.valueOf(str);
352             startTime();
353             for (int j = COUNT; --j >= 0;) {
354                 int index = MathLib.random(0, txt.length() - 1);
355                 txt = txt.delete(index, index + 1);
356             }
357             keepBestTime(COUNT);
358         }
359         println(endTime());
360
361         print("Text delete (stack): ");
362         for (int i = 0; i < 100; i++) {
363             PoolContext.enter();
364             Text txt = Text.valueOf(str);
365             startTime();
366             for (int j = COUNT; --j >= 0;) {
367                 int index = MathLib.random(0, txt.length() - 1);
368                 txt = txt.delete(index, index + 1);
369             }
370             keepBestTime(COUNT);
371             PoolContext.exit();
372         }
373         println(endTime());
374
375         println("");
376     }
377 }
Popular Tags