001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package simple.cpu;
042:
043: import java.io.FileNotFoundException;
044: import java.io.FileOutputStream;
045: import java.io.PrintStream;
046: import java.util.HashMap;
047:
048: /**
049: *
050: * @author ehucka
051: */
052: public class Measure {
053: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
054:
055: static PrintStream ps;
056:
057: //~ Constructors -------------------------------------------------------------------------------------------------------------
058:
059: /**
060: * Creates a new instance of Measure
061: */
062: public Measure() {
063: }
064:
065: //~ Methods ------------------------------------------------------------------------------------------------------------------
066:
067: public void empty() {
068: }
069:
070: public static void main(String[] args) {
071: System.out.println("Application started: "
072: + System.currentTimeMillis());
073:
074: try {
075: ps = new PrintStream(new FileOutputStream(args[0]));
076:
077: Measure m = new Measure();
078: m.test();
079: ps.close();
080: } catch (Exception ex) {
081: ex.printStackTrace();
082: }
083:
084: System.out.println("Application finished: "
085: + System.currentTimeMillis());
086: }
087:
088: public void run10() {
089: long time = System.currentTimeMillis();
090:
091: while ((System.currentTimeMillis() - time) < 10) {
092: ;
093: }
094: }
095:
096: public void run100() {
097: long time = System.currentTimeMillis();
098:
099: while ((System.currentTimeMillis() - time) < 100) {
100: ;
101: }
102: }
103:
104: public void run1000() {
105: long time = System.currentTimeMillis();
106:
107: while ((System.currentTimeMillis() - time) < 1000) {
108: ;
109: }
110: }
111:
112: public void run1000Call1() {
113: empty();
114:
115: long time = System.currentTimeMillis();
116:
117: while ((System.currentTimeMillis() - time) < 1000) {
118: ;
119: }
120: }
121:
122: public void run1000Call4() {
123: empty();
124: empty();
125:
126: long time = System.currentTimeMillis();
127:
128: while ((System.currentTimeMillis() - time) < 1000) {
129: ;
130: }
131:
132: empty();
133: empty();
134: }
135:
136: public void run100Call1() {
137: empty();
138:
139: long time = System.currentTimeMillis();
140:
141: while ((System.currentTimeMillis() - time) < 100) {
142: ;
143: }
144: }
145:
146: public void run100Call4() {
147: empty();
148: empty();
149:
150: long time = System.currentTimeMillis();
151:
152: while ((System.currentTimeMillis() - time) < 100) {
153: ;
154: }
155:
156: empty();
157: empty();
158: }
159:
160: //a method call
161: public void run10Call1() {
162: empty();
163:
164: long time = System.currentTimeMillis();
165:
166: while ((System.currentTimeMillis() - time) < 10) {
167: ;
168: }
169: }
170:
171: //4 calls
172: public void run10Call4() {
173: empty();
174: empty();
175:
176: long time = System.currentTimeMillis();
177:
178: while ((System.currentTimeMillis() - time) < 10) {
179: ;
180: }
181:
182: empty();
183: empty();
184: }
185:
186: public void run20() {
187: long time = System.currentTimeMillis();
188:
189: while ((System.currentTimeMillis() - time) < 20) {
190: ;
191: }
192: }
193:
194: public void run200() {
195: long time = System.currentTimeMillis();
196:
197: while ((System.currentTimeMillis() - time) < 200) {
198: ;
199: }
200: }
201:
202: public void run2000() {
203: long time = System.currentTimeMillis();
204:
205: while ((System.currentTimeMillis() - time) < 2000) {
206: ;
207: }
208: }
209:
210: public void run2000Call1() {
211: empty();
212:
213: long time = System.currentTimeMillis();
214:
215: while ((System.currentTimeMillis() - time) < 2000) {
216: ;
217: }
218: }
219:
220: public void run2000Call4() {
221: empty();
222: empty();
223:
224: long time = System.currentTimeMillis();
225:
226: while ((System.currentTimeMillis() - time) < 2000) {
227: ;
228: }
229:
230: empty();
231: empty();
232: }
233:
234: public void run200Call1() {
235: empty();
236:
237: long time = System.currentTimeMillis();
238:
239: while ((System.currentTimeMillis() - time) < 200) {
240: ;
241: }
242: }
243:
244: public void run200Call4() {
245: empty();
246: empty();
247:
248: long time = System.currentTimeMillis();
249:
250: while ((System.currentTimeMillis() - time) < 200) {
251: ;
252: }
253:
254: empty();
255: empty();
256: }
257:
258: public void run20Call1() {
259: empty();
260:
261: long time = System.currentTimeMillis();
262:
263: while ((System.currentTimeMillis() - time) < 20) {
264: ;
265: }
266: }
267:
268: public void run20Call4() {
269: empty();
270: empty();
271:
272: long time = System.currentTimeMillis();
273:
274: while ((System.currentTimeMillis() - time) < 20) {
275: ;
276: }
277:
278: empty();
279: empty();
280: }
281:
282: public void run4000by10() {
283: long time;
284:
285: for (int i = 0; i < 400; i++) {
286: time = System.currentTimeMillis();
287:
288: while ((System.currentTimeMillis() - time) < 10) {
289: ;
290: }
291: }
292: }
293:
294: public void run4000by100() {
295: long time;
296:
297: for (int i = 0; i < 40; i++) {
298: time = System.currentTimeMillis();
299:
300: while ((System.currentTimeMillis() - time) < 100) {
301: ;
302: }
303: }
304: }
305:
306: public void run4000by1000() {
307: long time;
308:
309: for (int i = 0; i < 4; i++) {
310: time = System.currentTimeMillis();
311:
312: while ((System.currentTimeMillis() - time) < 1000) {
313: ;
314: }
315: }
316: }
317:
318: public void test() {
319: ps.println("Method;Ideal Time;Measured time");
320:
321: long time = System.currentTimeMillis();
322: int count = 20;
323:
324: for (int i = 0; i < count; i++) {
325: run10();
326: }
327:
328: time = System.currentTimeMillis() - time;
329:
330: double val = time / (double) count;
331: ps.println("run10;10;" + String.valueOf(val));
332:
333: time = System.currentTimeMillis();
334: count = 20;
335:
336: for (int i = 0; i < count; i++) {
337: run20();
338: }
339:
340: time = System.currentTimeMillis() - time;
341: val = time / (double) count;
342: ps.println("run20;20;" + String.valueOf(val));
343:
344: time = System.currentTimeMillis();
345: count = 10;
346:
347: for (int i = 0; i < count; i++) {
348: run100();
349: }
350:
351: time = System.currentTimeMillis() - time;
352: val = time / (double) count;
353: ps.println("run100;100;" + String.valueOf(val));
354:
355: time = System.currentTimeMillis();
356: count = 10;
357:
358: for (int i = 0; i < count; i++) {
359: run200();
360: }
361:
362: time = System.currentTimeMillis() - time;
363: val = time / (double) count;
364: ps.println("run200;200;" + String.valueOf(val));
365:
366: time = System.currentTimeMillis();
367: count = 5;
368:
369: for (int i = 0; i < count; i++) {
370: run1000();
371: }
372:
373: time = System.currentTimeMillis() - time;
374: val = time / (double) count;
375: ps.println("run1000;1000;" + String.valueOf(val));
376:
377: time = System.currentTimeMillis();
378: count = 5;
379:
380: for (int i = 0; i < count; i++) {
381: run2000();
382: }
383:
384: time = System.currentTimeMillis() - time;
385: val = time / (double) count;
386: ps.println("run2000;2000;" + String.valueOf(val));
387:
388: time = System.currentTimeMillis();
389: count = 4;
390:
391: for (int i = 0; i < count; i++) {
392: run4000by10();
393: }
394:
395: time = System.currentTimeMillis() - time;
396: val = time / (double) count;
397: ps.println("run4000by10;4000;" + String.valueOf(val));
398:
399: time = System.currentTimeMillis();
400: count = 4;
401:
402: for (int i = 0; i < count; i++) {
403: run4000by100();
404: }
405:
406: time = System.currentTimeMillis() - time;
407: val = time / (double) count;
408: ps.println("run4000by100;4000;" + String.valueOf(val));
409:
410: time = System.currentTimeMillis();
411: count = 4;
412:
413: for (int i = 0; i < count; i++) {
414: run4000by1000();
415: }
416:
417: time = System.currentTimeMillis() - time;
418: val = time / (double) count;
419: ps.println("run4000by1000;4000;" + String.valueOf(val));
420:
421: //******************************************************************
422: time = System.currentTimeMillis();
423: count = 20;
424:
425: for (int i = 0; i < count; i++) {
426: run10Call1();
427: }
428:
429: time = System.currentTimeMillis() - time;
430: val = time / (double) count;
431: ps.println("run10Call1;10;" + String.valueOf(val));
432:
433: time = System.currentTimeMillis();
434: count = 20;
435:
436: for (int i = 0; i < count; i++) {
437: run20Call1();
438: }
439:
440: time = System.currentTimeMillis() - time;
441: val = time / (double) count;
442: ps.println("run20Call1;20;" + String.valueOf(val));
443:
444: time = System.currentTimeMillis();
445: count = 10;
446:
447: for (int i = 0; i < count; i++) {
448: run100Call1();
449: }
450:
451: time = System.currentTimeMillis() - time;
452: val = time / (double) count;
453: ps.println("run100Call1;100;" + String.valueOf(val));
454:
455: time = System.currentTimeMillis();
456: count = 10;
457:
458: for (int i = 0; i < count; i++) {
459: run200Call1();
460: }
461:
462: time = System.currentTimeMillis() - time;
463: val = time / (double) count;
464: ps.println("run200Call1;200;" + String.valueOf(val));
465:
466: time = System.currentTimeMillis();
467: count = 5;
468:
469: for (int i = 0; i < count; i++) {
470: run1000Call1();
471: }
472:
473: time = System.currentTimeMillis() - time;
474: val = time / (double) count;
475: ps.println("run1000Call1;1000;" + String.valueOf(val));
476:
477: time = System.currentTimeMillis();
478: count = 5;
479:
480: for (int i = 0; i < count; i++) {
481: run2000Call1();
482: }
483:
484: time = System.currentTimeMillis() - time;
485: val = time / (double) count;
486: ps.println("run2000Call1;2000;" + String.valueOf(val));
487: //******************************************************************
488: time = System.currentTimeMillis();
489: count = 20;
490:
491: for (int i = 0; i < count; i++) {
492: run10Call4();
493: }
494:
495: time = System.currentTimeMillis() - time;
496: val = time / (double) count;
497: ps.println("run10Call4;10;" + String.valueOf(val));
498:
499: time = System.currentTimeMillis();
500: count = 20;
501:
502: for (int i = 0; i < count; i++) {
503: run20Call4();
504: }
505:
506: time = System.currentTimeMillis() - time;
507: val = time / (double) count;
508: ps.println("run20Call4;20;" + String.valueOf(val));
509:
510: time = System.currentTimeMillis();
511: count = 10;
512:
513: for (int i = 0; i < count; i++) {
514: run100Call4();
515: }
516:
517: time = System.currentTimeMillis() - time;
518: val = time / (double) count;
519: ps.println("run100Call4;100;" + String.valueOf(val));
520:
521: time = System.currentTimeMillis();
522: count = 10;
523:
524: for (int i = 0; i < count; i++) {
525: run200Call4();
526: }
527:
528: time = System.currentTimeMillis() - time;
529: val = time / (double) count;
530: ps.println("run200Call4;200;" + String.valueOf(val));
531:
532: time = System.currentTimeMillis();
533: count = 5;
534:
535: for (int i = 0; i < count; i++) {
536: run1000Call4();
537: }
538:
539: time = System.currentTimeMillis() - time;
540: val = time / (double) count;
541: ps.println("run1000Call4;1000;" + String.valueOf(val));
542:
543: time = System.currentTimeMillis();
544: count = 5;
545:
546: for (int i = 0; i < count; i++) {
547: run2000Call4();
548: }
549:
550: time = System.currentTimeMillis() - time;
551: val = time / (double) count;
552: ps.println("run2000Call4;2000;" + String.valueOf(val));
553:
554: //******************************************************************
555: }
556:
557: protected static String complete(String s, int chars) {
558: StringBuffer sb = new StringBuffer(chars);
559: int tot = chars - s.length();
560: sb.append(s);
561:
562: for (int i = 0; i < tot; i++) {
563: sb.append(" ");
564: }
565:
566: return sb.substring(0, chars);
567: }
568: }
|