KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > performance > NewVsSetLen


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.performance;
18
19 /**
20
21    This program compares the cost of creating a new StringBuffer and
22    converting it to a String versus keeping the same StringBuffer,
23    setting its size to zero and then converting it to String.
24
25    <p>The table below gives some figures.
26
27 <p> <table border="1" cellpadding="4">
28    <tr BGCOLOR="#33CCFF">
29      <th BGCOLOR="#CCCCCC" rowspan="2">Total Message length
30      <th colspan="2" align="center">0
31      <th colspan="2" align="center">1
32      <th colspan="2" align="center">2
33      <th colspan="2" align="center">4
34      <th colspan="2" align="center">8
35    </tr>
36
37    <tr BGCOLOR="#3366FF">
38      <td>New Buffer</td> <td>setLength</td>
39      <td>New Buffer</td> <td>setLength</td>
40      <td>New Buffer</td> <td>setLength</td>
41      <td>New Buffer</td> <td>setLength</td>
42      <td>New Buffer</td> <td>setLength</td>
43    </tr>
44
45    <tr align="right">
46    <td BGCOLOR="#CCCCCC">256
47    <td>33 <td>22
48    <td>34 <td>22
49    <td>34 <td>22
50    <td>34 <td>22
51    <td>33 <td>23
52    </tr>
53
54    <tr align="right">
55    <td BGCOLOR="#CCCCCC">1024
56    <td>58 <td>41
57    <td>59 <td>45
58    <td>59 <td>48
59    <td>59 <td>51
60    <td>60 <td>44
61    </tr>
62
63    <tr align="right">
64    <td BGCOLOR="#CCCCCC">4096
65    <td>146 <td>132
66    <td>138 <td>132
67    <td>144 <td>126
68    <td>142 <td>132
69    <td>136 <td>132
70    </tr>
71
72    <tr align="right">
73    <td BGCOLOR="#CCCCCC">16384
74    <td>617 <td>593
75    <td>593 <td>609
76    <td>601 <td>617
77    <td>601 <td>632
78    <td>593 <td>632
79    </tr>
80
81    <tr align="right">
82    <td BGCOLOR="#CCCCCC">65536
83    <td>3218 <td>3281
84    <td>3093 <td>3125
85    <td>3125 <td>3156
86    <td>3125 <td>3281
87    <td>3062 <td>3562
88    </tr>
89
90    <tr align="right">
91    <td BGCOLOR="#CCCCCC">262144
92    <td>14750 <td>15125
93    <td>14000 <td>15500
94    <td>14000 <td>16125
95    <td>14000 <td>18000
96    <td>14000 <td>21375
97    </tr>
98
99    <tr align="right">
100    <td BGCOLOR="#CCCCCC">1048576
101    <td>87500 <td>80000
102    <td>60500 <td>82000
103    <td>57000 <td>93000
104    <td>57500 <td>118500
105    <td>57500 <td>168500
106    </tr>
107
108    <caption ALIGN="BOTTOM">Performance comparisons of new buffer
109    creation versus setLength(0) approach for various message sizes and
110    secondary loop lengths.
111    </caption>
112    </table>
113
114    <p>The tests copy a message to a destination string buffer and then
115    copy a 256 character buffer to another buffer the number of times
116    as specified by the secondary loop length.
117
118
119    <p>The <code>setLength(0)</code> method is usually faster. However,
120    after copying a large string it becomes slow even when copying
121    small strings.
122
123
124    <p>This is due to a peculiarity in the <code>copy</code> method in
125    StringBuffer class which creates a character array of the same
126    length as the old buffer even if the vast majority of those
127    characters are unused.
128
129    <p>The tests were performed on Linux using IBM's JDK 1.1.8.
130
131    <p>The test script is a crude model of what might happen in
132    reality. If you remain unconvinced of its results, then please send
133    your alternative measurement scenario.
134
135    
136
137    
138 */

139 public class NewVsSetLen {
140
141   static String JavaDoc s;
142
143   static int BIGBUF_LEN = 1048576;
144   static int SBUF_LEN = 256;
145   static int RUN_LENGTH = BIGBUF_LEN/4;
146
147   static char[] sbuf = new char[SBUF_LEN];
148   static char[] bigbuf = new char[BIGBUF_LEN];
149
150   {
151     for(int i = 0; i < SBUF_LEN; i++) {
152       sbuf[i] = (char) (i);
153     }
154
155     for(int i = 0; i < BIGBUF_LEN; i++) {
156       bigbuf[i] = (char) (i);
157     }
158   }
159
160
161   static
162   public
163   void main(String JavaDoc[] args) {
164  
165     int t;
166
167     for(int len = SBUF_LEN; len <= BIGBUF_LEN; len*=4, RUN_LENGTH /= 4) {
168       System.out.println("<td>"+len+"\n");
169       for(int second = 0; second < 16;) {
170     System.out.println("SECOND loop="+second +", RUN_LENGTH="
171                +RUN_LENGTH+", len="+len);
172     t = (int)newBuffer(len, second);
173
174     System.out.print("<td>" + t);
175     t = (int)setLen(len, second);
176     System.out.println(" <td>" + t + " \n");
177     if(second == 0) {
178       second = 1;
179     } else {
180       second *= 2;
181     }
182       }
183     }
184     
185   }
186
187   static
188   double newBuffer(int size, int second) {
189     long before = System.currentTimeMillis();
190
191     for(int i = 0; i < RUN_LENGTH; i++) {
192       StringBuffer JavaDoc buf = new StringBuffer JavaDoc(SBUF_LEN);
193       buf.append(sbuf, 0, sbuf.length);
194       buf.append(bigbuf, 0, size);
195       s = buf.toString();
196     }
197
198     for(int x = 0; x < second; x++) {
199       StringBuffer JavaDoc buf = new StringBuffer JavaDoc(SBUF_LEN);
200       buf.append(sbuf, 0, SBUF_LEN);
201       s = buf.toString();
202     }
203     return (System.currentTimeMillis() - before)*1000.0/RUN_LENGTH;
204   }
205
206   static
207   double setLen(int size, int second) {
208     long before = System.currentTimeMillis();
209
210     StringBuffer JavaDoc buf = new StringBuffer JavaDoc(SBUF_LEN);
211
212     for(int i = 0; i < RUN_LENGTH; i++) {
213       buf.append(sbuf, 0, sbuf.length);
214       buf.append(bigbuf, 0, size);
215       s = buf.toString();
216       buf.setLength(0);
217     }
218
219     for(int x = 0; x < second; x++) {
220       buf.append(sbuf, 0, SBUF_LEN);
221       s = buf.toString();
222       buf.setLength(0);
223     }
224     return (System.currentTimeMillis() - before)*1000.0/RUN_LENGTH;
225   }
226 }
227
Popular Tags