1 16 17 package org.apache.log4j.performance; 18 19 139 public class NewVsSetLen { 140 141 static String 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 [] 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 buf = new StringBuffer (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 buf = new StringBuffer (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 buf = new StringBuffer (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 |