0001: /*
0002: * Copyright 2001-2005 Stephen Colebourne
0003: *
0004: * Licensed under the Apache License, Version 2.0 (the "License");
0005: * you may not use this file except in compliance with the License.
0006: * You may obtain a copy of the License at
0007: *
0008: * http://www.apache.org/licenses/LICENSE-2.0
0009: *
0010: * Unless required by applicable law or agreed to in writing, software
0011: * distributed under the License is distributed on an "AS IS" BASIS,
0012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013: * See the License for the specific language governing permissions and
0014: * limitations under the License.
0015: */
0016: package org.joda.example.time;
0017:
0018: import java.text.SimpleDateFormat;
0019: import java.util.ArrayList;
0020: import java.util.Date;
0021: import java.util.GregorianCalendar;
0022: import java.util.HashMap;
0023: import java.util.Iterator;
0024: import java.util.List;
0025: import java.util.Map;
0026:
0027: import org.joda.time.DateTime;
0028: import org.joda.time.MutableDateTime;
0029: import org.joda.time.chrono.GJChronology;
0030: import org.joda.time.format.DateTimeFormat;
0031: import org.joda.time.format.DateTimeFormatter;
0032:
0033: /**
0034: * DateTimePerformance provides various comparisons between the Java supplied
0035: * Date classes and the Joda ones.
0036: *
0037: * @author Stephen Colebourne
0038: */
0039: public class DateTimePerformance {
0040:
0041: private static class Result {
0042: String object = null;
0043: String name = null;
0044: long time = 0;
0045: long avg = 0;
0046: int runs = 0;
0047: }
0048:
0049: private static int AVERAGE = 3;
0050: private static int COUNT_VERY_FAST = 5000000;
0051: private static int COUNT_FAST = 200000;
0052: private static int COUNT_SLOW = 20000;
0053:
0054: private Map results = new HashMap();
0055: private List resultList = new ArrayList();
0056:
0057: private Result result = null;
0058: private long start = 0;
0059: private long end = 0;
0060:
0061: /**
0062: * Constructor
0063: */
0064: public static void main(String[] args) {
0065: try {
0066: new DateTimePerformance();
0067:
0068: } catch (Throwable th) {
0069: th.printStackTrace();
0070: }
0071: }
0072:
0073: /**
0074: * Constructor
0075: */
0076: public DateTimePerformance() throws Exception {
0077: checkJodaConstructor1();
0078: checkJISOConstructor1();
0079: checkGCalConstructor1();
0080: checkDateConstructor1();
0081:
0082: checkJodaConstructor2();
0083: checkJISOConstructor2();
0084: checkGCalConstructor2();
0085: checkDateConstructor2();
0086:
0087: checkJodaConstructor3();
0088: checkJISOConstructor3();
0089: checkGCalConstructor3();
0090: checkDateConstructor3();
0091:
0092: checkJodaGetYear();
0093: checkJISOGetYear();
0094: checkGCalGetYear();
0095: checkDateGetYear();
0096:
0097: // checkJodaGetMonth();
0098: // checkJISOGetMonth();
0099: // checkGCalGetMonth();
0100: // checkDateGetMonth();
0101:
0102: // checkJodaGetDay();
0103: // checkJISOGetDay();
0104: // checkGCalGetDay();
0105: // checkDateGetDay();
0106:
0107: checkJodaGetHour();
0108: checkJISOGetHour();
0109: checkGCalGetHour();
0110: checkDateGetHour();
0111:
0112: checkJodaSetYear();
0113: checkJISOSetYear();
0114: checkGCalSetYear();
0115: checkDateSetYear();
0116:
0117: checkJodaSetGetYear();
0118: checkJISOSetGetYear();
0119: checkGCalSetGetYear();
0120: checkDateSetGetYear();
0121:
0122: checkJodaSetHour();
0123: checkJISOSetHour();
0124: checkGCalSetHour();
0125: checkDateSetHour();
0126:
0127: checkJodaSetGetHour();
0128: checkJISOSetGetHour();
0129: checkGCalSetGetHour();
0130: checkDateSetGetHour();
0131:
0132: checkJodaToString();
0133: checkJISOToString();
0134: checkGCalToString();
0135: checkDateToString();
0136:
0137: System.out.println("");
0138: long jodaTotal = 0;
0139: long jisoTotal = 0;
0140: long gcalTotal = 0;
0141: long dateTotal = 0;
0142: for (Iterator it = resultList.iterator(); it.hasNext();) {
0143: Result res = (Result) it.next();
0144: System.out.println(res.object + "." + res.name + ": "
0145: + res.avg + "ns");
0146: if (res.object.equals("Joda")) {
0147: jodaTotal += res.avg;
0148: } else if (res.object.equals("JISO")) {
0149: jisoTotal += res.avg;
0150: } else if (res.object.equals("GCal")) {
0151: gcalTotal += res.avg;
0152: } else if (res.object.equals("Date")) {
0153: dateTotal += res.avg;
0154: System.out.println("");
0155: }
0156: }
0157: System.out.println("Joda: " + jodaTotal);
0158: System.out.println("JISO: " + jisoTotal);
0159: System.out.println("GCal: " + gcalTotal);
0160: System.out.println("Date: " + dateTotal);
0161: }
0162:
0163: // Constructor using currentTimeMillis()
0164: //------------------------------------------------------------------------
0165:
0166: private void checkJodaConstructor1() {
0167: int COUNT = COUNT_SLOW;
0168: DateTime dt = new DateTime(GJChronology.getInstance());
0169: int count = 0;
0170: for (int i = 0; i < AVERAGE; i++) {
0171: start("Joda", "new()");
0172: for (int j = 0; j < COUNT; j++) {
0173: dt = new DateTime(GJChronology.getInstance());
0174: if (count++ < 0) {
0175: System.out.println("Anti optimise");
0176: }
0177: }
0178: end(COUNT);
0179: }
0180: }
0181:
0182: private void checkJISOConstructor1() {
0183: int COUNT = COUNT_SLOW;
0184: DateTime dt = new DateTime();
0185: int count = 0;
0186: for (int i = 0; i < AVERAGE; i++) {
0187: start("JISO", "new()");
0188: for (int j = 0; j < COUNT; j++) {
0189: dt = new DateTime();
0190: if (count++ < 0) {
0191: System.out.println("Anti optimise");
0192: }
0193: }
0194: end(COUNT);
0195: }
0196: }
0197:
0198: private void checkGCalConstructor1() {
0199: int COUNT = COUNT_SLOW;
0200: GregorianCalendar dt = new GregorianCalendar();
0201: int count = 0;
0202: for (int i = 0; i < AVERAGE; i++) {
0203: start("GCal", "new()");
0204: for (int j = 0; j < COUNT; j++) {
0205: dt = new GregorianCalendar();
0206: if (count++ < 0) {
0207: System.out.println("Anti optimise");
0208: }
0209: }
0210: end(COUNT);
0211: }
0212: }
0213:
0214: private void checkDateConstructor1() {
0215: int COUNT = COUNT_SLOW;
0216: Date dt = new Date();
0217: int count = 0;
0218: for (int i = 0; i < AVERAGE; i++) {
0219: start("Date", "new()");
0220: for (int j = 0; j < COUNT; j++) {
0221: dt = new Date();
0222: if (count++ < 0) {
0223: System.out.println("Anti optimise");
0224: }
0225: }
0226: end(COUNT);
0227: }
0228: }
0229:
0230: // Constructor using long millis
0231: //------------------------------------------------------------------------
0232:
0233: private void checkJodaConstructor2() {
0234: int COUNT = COUNT_VERY_FAST;
0235: DateTime dt = new DateTime(12345L, GJChronology.getInstance());
0236: for (int i = 0; i < AVERAGE; i++) {
0237: start("Joda", "new(millis)");
0238: for (int j = 0; j < COUNT; j++) {
0239: dt = new DateTime(12345L, GJChronology.getInstance());
0240: if (dt == null) {
0241: System.out.println("Anti optimise");
0242: }
0243: }
0244: end(COUNT);
0245: }
0246: }
0247:
0248: private void checkJISOConstructor2() {
0249: int COUNT = COUNT_VERY_FAST;
0250: DateTime dt = new DateTime(12345L);
0251: for (int i = 0; i < AVERAGE; i++) {
0252: start("JISO", "new(millis)");
0253: for (int j = 0; j < COUNT; j++) {
0254: dt = new DateTime(12345L);
0255: if (dt == null) {
0256: System.out.println("Anti optimise");
0257: }
0258: }
0259: end(COUNT);
0260: }
0261: }
0262:
0263: private void checkGCalConstructor2() {
0264: int COUNT = COUNT_SLOW;
0265: GregorianCalendar dt = new GregorianCalendar();
0266: for (int i = 0; i < AVERAGE; i++) {
0267: start("GCal", "new(millis)");
0268: for (int j = 0; j < COUNT; j++) {
0269: dt = new GregorianCalendar();
0270: dt.setTime(new Date(12345L));
0271: if (dt == null) {
0272: System.out.println("Anti optimise");
0273: }
0274: }
0275: end(COUNT);
0276: }
0277: }
0278:
0279: private void checkDateConstructor2() {
0280: int COUNT = COUNT_VERY_FAST;
0281: Date dt = new Date();
0282: for (int i = 0; i < AVERAGE; i++) {
0283: start("Date", "new(millis)");
0284: for (int j = 0; j < COUNT; j++) {
0285: dt = new Date(12345L);
0286: if (dt == null) {
0287: System.out.println("Anti optimise");
0288: }
0289: }
0290: end(COUNT);
0291: }
0292: }
0293:
0294: // Constructor using year month and day
0295: //------------------------------------------------------------------------
0296:
0297: private void checkJodaConstructor3() {
0298: int COUNT = COUNT_SLOW;
0299: DateTime dt = new DateTime(1972, 10, 1, 0, 0, 0, 0,
0300: GJChronology.getInstance());
0301: for (int i = 0; i < AVERAGE; i++) {
0302: start("Joda", "new(YMD)");
0303: for (int j = 0; j < COUNT; j++) {
0304: dt = new DateTime(1972, 10, 1, 0, 0, 0, 0, GJChronology
0305: .getInstance());
0306: if (dt == null) {
0307: System.out.println("Anti optimise");
0308: }
0309: }
0310: end(COUNT);
0311: }
0312: }
0313:
0314: private void checkJISOConstructor3() {
0315: int COUNT = COUNT_SLOW;
0316: DateTime dt = new DateTime(1972, 10, 1, 0, 0, 0, 0);
0317: for (int i = 0; i < AVERAGE; i++) {
0318: start("JISO", "new(YMD)");
0319: for (int j = 0; j < COUNT; j++) {
0320: dt = new DateTime(1972, 10, 1, 0, 0, 0, 0);
0321: if (dt == null) {
0322: System.out.println("Anti optimise");
0323: }
0324: }
0325: end(COUNT);
0326: }
0327: }
0328:
0329: private void checkGCalConstructor3() {
0330: int COUNT = COUNT_SLOW;
0331: GregorianCalendar dt = new GregorianCalendar(1972, 10, 1);
0332: for (int i = 0; i < AVERAGE; i++) {
0333: start("GCal", "new(YMD)");
0334: for (int j = 0; j < COUNT; j++) {
0335: dt = new GregorianCalendar(1972, 10, 1);
0336: if (dt == null) {
0337: System.out.println("Anti optimise");
0338: }
0339: }
0340: end(COUNT);
0341: }
0342: }
0343:
0344: private void checkDateConstructor3() {
0345: int COUNT = COUNT_SLOW;
0346: Date dt = new Date();
0347: for (int i = 0; i < AVERAGE; i++) {
0348: start("Date", "new(YMD)");
0349: for (int j = 0; j < COUNT; j++) {
0350: dt = new Date(1972, 10, 1);
0351: if (dt == null) {
0352: System.out.println("Anti optimise");
0353: }
0354: }
0355: end(COUNT);
0356: }
0357: }
0358:
0359: // Get year
0360: //------------------------------------------------------------------------
0361:
0362: private void checkJodaGetYear() {
0363: int COUNT = COUNT_VERY_FAST;
0364: DateTime dt = new DateTime(GJChronology.getInstance());
0365: for (int i = 0; i < AVERAGE; i++) {
0366: start("Joda", "getYear");
0367: for (int j = 0; j < COUNT; j++) {
0368: int val = dt.getYear();
0369: if (val == 0) {
0370: System.out.println("Anti optimise");
0371: }
0372: }
0373: end(COUNT);
0374: }
0375: }
0376:
0377: private void checkJISOGetYear() {
0378: int COUNT = COUNT_VERY_FAST;
0379: DateTime dt = new DateTime();
0380: for (int i = 0; i < AVERAGE; i++) {
0381: start("JISO", "getYear");
0382: for (int j = 0; j < COUNT; j++) {
0383: int val = dt.getYear();
0384: if (val == 0) {
0385: System.out.println("Anti optimise");
0386: }
0387: }
0388: end(COUNT);
0389: }
0390: }
0391:
0392: private void checkGCalGetYear() {
0393: int COUNT = COUNT_VERY_FAST;
0394: GregorianCalendar dt = new GregorianCalendar();
0395: for (int i = 0; i < AVERAGE; i++) {
0396: start("GCal", "getYear");
0397: for (int j = 0; j < COUNT; j++) {
0398: int val = dt.get(GregorianCalendar.YEAR);
0399: if (val == 0) {
0400: System.out.println("Anti optimise");
0401: }
0402: }
0403: end(COUNT);
0404: }
0405: }
0406:
0407: private void checkDateGetYear() {
0408: int COUNT = COUNT_FAST;
0409: Date dt = new Date();
0410: for (int i = 0; i < AVERAGE; i++) {
0411: start("Date", "getYear");
0412: for (int j = 0; j < COUNT; j++) {
0413: int val = dt.getYear();
0414: if (val == 0) {
0415: System.out.println("Anti optimise");
0416: }
0417: }
0418: end(COUNT);
0419: }
0420: }
0421:
0422: // Get month
0423: //------------------------------------------------------------------------
0424:
0425: private void checkJodaGetMonth() {
0426: int COUNT = COUNT_VERY_FAST;
0427: DateTime dt = new DateTime(GJChronology.getInstance());
0428: for (int i = 0; i < AVERAGE; i++) {
0429: start("Joda", "getMonth");
0430: for (int j = 0; j < COUNT; j++) {
0431: int val = dt.getMonthOfYear();
0432: if (val == 0) {
0433: System.out.println("Anti optimise");
0434: }
0435: }
0436: end(COUNT);
0437: }
0438: }
0439:
0440: private void checkJISOGetMonth() {
0441: int COUNT = COUNT_VERY_FAST;
0442: DateTime dt = new DateTime();
0443: for (int i = 0; i < AVERAGE; i++) {
0444: start("JISO", "getMonth");
0445: for (int j = 0; j < COUNT; j++) {
0446: int val = dt.getMonthOfYear();
0447: if (val == 0) {
0448: System.out.println("Anti optimise");
0449: }
0450: }
0451: end(COUNT);
0452: }
0453: }
0454:
0455: private void checkGCalGetMonth() {
0456: int COUNT = COUNT_VERY_FAST;
0457: GregorianCalendar dt = new GregorianCalendar();
0458: for (int i = 0; i < AVERAGE; i++) {
0459: start("GCal", "getMonth");
0460: for (int j = 0; j < COUNT; j++) {
0461: int val = dt.get(GregorianCalendar.MONTH);
0462: if (val == 0) {
0463: System.out.println("Anti optimise");
0464: }
0465: }
0466: end(COUNT);
0467: }
0468: }
0469:
0470: private void checkDateGetMonth() {
0471: int COUNT = COUNT_FAST;
0472: Date dt = new Date();
0473: for (int i = 0; i < AVERAGE; i++) {
0474: start("Date", "getMonth");
0475: for (int j = 0; j < COUNT; j++) {
0476: int val = dt.getMonth();
0477: if (val == 0) {
0478: System.out.println("Anti optimise");
0479: }
0480: }
0481: end(COUNT);
0482: }
0483: }
0484:
0485: // Get day
0486: //------------------------------------------------------------------------
0487:
0488: private void checkJodaGetDay() {
0489: int COUNT = COUNT_VERY_FAST;
0490: DateTime dt = new DateTime(GJChronology.getInstance());
0491: for (int i = 0; i < AVERAGE; i++) {
0492: start("Joda", "getDay");
0493: for (int j = 0; j < COUNT; j++) {
0494: int val = dt.getDayOfMonth();
0495: if (val == 0) {
0496: System.out.println("Anti optimise");
0497: }
0498: }
0499: end(COUNT);
0500: }
0501: }
0502:
0503: private void checkJISOGetDay() {
0504: int COUNT = COUNT_VERY_FAST;
0505: DateTime dt = new DateTime();
0506: for (int i = 0; i < AVERAGE; i++) {
0507: start("JISO", "getDay");
0508: for (int j = 0; j < COUNT; j++) {
0509: int val = dt.getDayOfMonth();
0510: if (val == 0) {
0511: System.out.println("Anti optimise");
0512: }
0513: }
0514: end(COUNT);
0515: }
0516: }
0517:
0518: private void checkGCalGetDay() {
0519: int COUNT = COUNT_VERY_FAST;
0520: GregorianCalendar dt = new GregorianCalendar();
0521: for (int i = 0; i < AVERAGE; i++) {
0522: start("GCal", "getDay");
0523: for (int j = 0; j < COUNT; j++) {
0524: int val = dt.get(GregorianCalendar.DAY_OF_MONTH);
0525: if (val == 0) {
0526: System.out.println("Anti optimise");
0527: }
0528: }
0529: end(COUNT);
0530: }
0531: }
0532:
0533: private void checkDateGetDay() {
0534: int COUNT = COUNT_FAST;
0535: Date dt = new Date();
0536: for (int i = 0; i < AVERAGE; i++) {
0537: start("Date", "getDay");
0538: for (int j = 0; j < COUNT; j++) {
0539: int val = dt.getDate();
0540: if (val == 0) {
0541: System.out.println("Anti optimise");
0542: }
0543: }
0544: end(COUNT);
0545: }
0546: }
0547:
0548: // Get hour
0549: //------------------------------------------------------------------------
0550:
0551: private void checkJodaGetHour() {
0552: int COUNT = COUNT_VERY_FAST;
0553: DateTime dt = new DateTime(GJChronology.getInstance());
0554: for (int i = 0; i < AVERAGE; i++) {
0555: start("Joda", "getHour");
0556: for (int j = 0; j < COUNT; j++) {
0557: int val = dt.getHourOfDay();
0558: if (val == -1) {
0559: System.out.println("Anti optimise");
0560: }
0561: }
0562: end(COUNT);
0563: }
0564: }
0565:
0566: private void checkJISOGetHour() {
0567: int COUNT = COUNT_VERY_FAST;
0568: DateTime dt = new DateTime();
0569: for (int i = 0; i < AVERAGE; i++) {
0570: start("JISO", "getHour");
0571: for (int j = 0; j < COUNT; j++) {
0572: int val = dt.getHourOfDay();
0573: if (val == -1) {
0574: System.out.println("Anti optimise");
0575: }
0576: }
0577: end(COUNT);
0578: }
0579: }
0580:
0581: private void checkGCalGetHour() {
0582: int COUNT = COUNT_VERY_FAST;
0583: GregorianCalendar dt = new GregorianCalendar();
0584: for (int i = 0; i < AVERAGE; i++) {
0585: start("GCal", "getHour");
0586: for (int j = 0; j < COUNT; j++) {
0587: int val = dt.get(GregorianCalendar.HOUR_OF_DAY);
0588: if (val == -1) {
0589: System.out.println("Anti optimise");
0590: }
0591: }
0592: end(COUNT);
0593: }
0594: }
0595:
0596: private void checkDateGetHour() {
0597: int COUNT = COUNT_FAST;
0598: Date dt = new Date();
0599: for (int i = 0; i < AVERAGE; i++) {
0600: start("Date", "getHour");
0601: for (int j = 0; j < COUNT; j++) {
0602: int val = dt.getHours();
0603: if (val == -1) {
0604: System.out.println("Anti optimise");
0605: }
0606: }
0607: end(COUNT);
0608: }
0609: }
0610:
0611: // Set year
0612: //------------------------------------------------------------------------
0613:
0614: private void checkJodaSetYear() {
0615: int COUNT = COUNT_FAST;
0616: // Is it fair to use only MutableDateTime here? You decide.
0617: MutableDateTime dt = new MutableDateTime(GJChronology
0618: .getInstance());
0619: for (int i = 0; i < AVERAGE; i++) {
0620: start("Joda", "setYear");
0621: for (int j = 0; j < COUNT; j++) {
0622: dt.setYear(1972);
0623: if (dt == null) {
0624: System.out.println("Anti optimise");
0625: }
0626: }
0627: end(COUNT);
0628: }
0629: }
0630:
0631: private void checkJISOSetYear() {
0632: int COUNT = COUNT_FAST;
0633: // Is it fair to use only MutableDateTime here? You decide.
0634: MutableDateTime dt = new MutableDateTime();
0635: for (int i = 0; i < AVERAGE; i++) {
0636: start("JISO", "setYear");
0637: for (int j = 0; j < COUNT; j++) {
0638: dt.setYear(1972);
0639: if (dt == null) {
0640: System.out.println("Anti optimise");
0641: }
0642: }
0643: end(COUNT);
0644: }
0645: }
0646:
0647: private void checkGCalSetYear() {
0648: int COUNT = COUNT_VERY_FAST;
0649: GregorianCalendar dt = new GregorianCalendar();
0650: for (int i = 0; i < AVERAGE; i++) {
0651: start("GCal", "setYear");
0652: for (int j = 0; j < COUNT; j++) {
0653: dt.set(GregorianCalendar.YEAR, 1972);
0654: if (dt == null) {
0655: System.out.println("Anti optimise");
0656: }
0657: }
0658: end(COUNT);
0659: }
0660: }
0661:
0662: private void checkDateSetYear() {
0663: int COUNT = COUNT_FAST;
0664: Date dt = new Date();
0665: for (int i = 0; i < AVERAGE; i++) {
0666: start("Date", "setYear");
0667: for (int j = 0; j < COUNT; j++) {
0668: dt.setYear(1972);
0669: if (dt == null) {
0670: System.out.println("Anti optimise");
0671: }
0672: }
0673: end(COUNT);
0674: }
0675: }
0676:
0677: // Set then get year
0678: //------------------------------------------------------------------------
0679:
0680: private void checkJodaSetGetYear() {
0681: int COUNT = COUNT_FAST;
0682: // Is it fair to use only MutableDateTime here? You decide.
0683: // MutableDateTime dt = new MutableDateTime(GJChronology.getInstance());
0684: // for (int i = 0; i < AVERAGE; i++) {
0685: // start("Joda", "setGetYear");
0686: // for (int j = 0; j < COUNT; j++) {
0687: // dt.setYear(1972);
0688: // int val = dt.getYear();
0689: // if (val < 0) {System.out.println("Anti optimise");}
0690: // }
0691: // end(COUNT);
0692: // }
0693: DateTime dt = new DateTime(GJChronology.getInstance());
0694: for (int i = 0; i < AVERAGE; i++) {
0695: start("Joda", "setGetYear");
0696: for (int j = 0; j < COUNT; j++) {
0697: dt = dt.year().setCopy(1972);
0698: int val = dt.getYear();
0699: if (val < 0) {
0700: System.out.println("Anti optimise");
0701: }
0702: }
0703: end(COUNT);
0704: }
0705: }
0706:
0707: private void checkJISOSetGetYear() {
0708: int COUNT = COUNT_FAST;
0709: // Is it fair to use only MutableDateTime here? You decide.
0710: // MutableDateTime dt = new MutableDateTime();
0711: // for (int i = 0; i < AVERAGE; i++) {
0712: // start("JISO", "setGetYear");
0713: // for (int j = 0; j < COUNT; j++) {
0714: // dt.setYear(1972);
0715: // int val = dt.getYear();
0716: // if (val < 0) {System.out.println("Anti optimise");}
0717: // }
0718: // end(COUNT);
0719: // }
0720: DateTime dt = new DateTime();
0721: for (int i = 0; i < AVERAGE; i++) {
0722: start("JISO", "setGetYear");
0723: for (int j = 0; j < COUNT; j++) {
0724: dt = dt.year().setCopy(1972);
0725: int val = dt.getYear();
0726: if (val < 0) {
0727: System.out.println("Anti optimise");
0728: }
0729: }
0730: end(COUNT);
0731: }
0732: }
0733:
0734: private void checkGCalSetGetYear() {
0735: int COUNT = COUNT_FAST;
0736: GregorianCalendar dt = new GregorianCalendar();
0737: for (int i = 0; i < AVERAGE; i++) {
0738: start("GCal", "setGetYear");
0739: for (int j = 0; j < COUNT; j++) {
0740: dt.set(GregorianCalendar.YEAR, 1972);
0741: int val = dt.get(GregorianCalendar.YEAR);
0742: if (val < 0) {
0743: System.out.println("Anti optimise");
0744: }
0745: }
0746: end(COUNT);
0747: }
0748: }
0749:
0750: private void checkDateSetGetYear() {
0751: int COUNT = COUNT_FAST;
0752: Date dt = new Date();
0753: for (int i = 0; i < AVERAGE; i++) {
0754: start("Date", "setGetYear");
0755: for (int j = 0; j < COUNT; j++) {
0756: dt.setYear(1972);
0757: int val = dt.getYear();
0758: if (val < 0) {
0759: System.out.println("Anti optimise");
0760: }
0761: }
0762: end(COUNT);
0763: }
0764: }
0765:
0766: // Set hour
0767: //------------------------------------------------------------------------
0768:
0769: private void checkJodaSetHour() {
0770: int COUNT = COUNT_VERY_FAST;
0771: // Is it fair to use only MutableDateTime here? You decide.
0772: MutableDateTime dt = new MutableDateTime(GJChronology
0773: .getInstance());
0774: for (int i = 0; i < AVERAGE; i++) {
0775: start("Joda", "setHour");
0776: for (int j = 0; j < COUNT; j++) {
0777: dt.setHourOfDay(13);
0778: if (dt == null) {
0779: System.out.println("Anti optimise");
0780: }
0781: }
0782: end(COUNT);
0783: }
0784: }
0785:
0786: private void checkJISOSetHour() {
0787: int COUNT = COUNT_VERY_FAST;
0788: // Is it fair to use only MutableDateTime here? You decide.
0789: MutableDateTime dt = new MutableDateTime();
0790: for (int i = 0; i < AVERAGE; i++) {
0791: start("JISO", "setHour");
0792: for (int j = 0; j < COUNT; j++) {
0793: dt.setHourOfDay(13);
0794: if (dt == null) {
0795: System.out.println("Anti optimise");
0796: }
0797: }
0798: end(COUNT);
0799: }
0800: }
0801:
0802: private void checkGCalSetHour() {
0803: int COUNT = COUNT_VERY_FAST;
0804: GregorianCalendar dt = new GregorianCalendar();
0805: for (int i = 0; i < AVERAGE; i++) {
0806: start("GCal", "setHour");
0807: for (int j = 0; j < COUNT; j++) {
0808: dt.set(GregorianCalendar.HOUR_OF_DAY, 13);
0809: if (dt == null) {
0810: System.out.println("Anti optimise");
0811: }
0812: }
0813: end(COUNT);
0814: }
0815: }
0816:
0817: private void checkDateSetHour() {
0818: int COUNT = COUNT_FAST;
0819: Date dt = new Date();
0820: for (int i = 0; i < AVERAGE; i++) {
0821: start("Date", "setHour");
0822: for (int j = 0; j < COUNT; j++) {
0823: dt.setHours(13);
0824: if (dt == null) {
0825: System.out.println("Anti optimise");
0826: }
0827: }
0828: end(COUNT);
0829: }
0830: }
0831:
0832: // Set hour
0833: //------------------------------------------------------------------------
0834:
0835: private void checkJodaSetGetHour() {
0836: int COUNT = COUNT_VERY_FAST;
0837: // Is it fair to use only MutableDateTime here? You decide.
0838: MutableDateTime dt = new MutableDateTime(GJChronology
0839: .getInstance());
0840: for (int i = 0; i < AVERAGE; i++) {
0841: start("Joda", "setGetHour");
0842: for (int j = 0; j < COUNT; j++) {
0843: dt.setHourOfDay(13);
0844: int val = dt.getHourOfDay();
0845: if (dt == null) {
0846: System.out.println("Anti optimise");
0847: }
0848: }
0849: end(COUNT);
0850: }
0851: }
0852:
0853: private void checkJISOSetGetHour() {
0854: int COUNT = COUNT_VERY_FAST;
0855: // Is it fair to use only MutableDateTime here? You decide.
0856: MutableDateTime dt = new MutableDateTime();
0857: for (int i = 0; i < AVERAGE; i++) {
0858: start("JISO", "setGetHour");
0859: for (int j = 0; j < COUNT; j++) {
0860: dt.setHourOfDay(13);
0861: int val = dt.getHourOfDay();
0862: if (dt == null) {
0863: System.out.println("Anti optimise");
0864: }
0865: }
0866: end(COUNT);
0867: }
0868: }
0869:
0870: private void checkGCalSetGetHour() {
0871: int COUNT = COUNT_VERY_FAST;
0872: GregorianCalendar dt = new GregorianCalendar();
0873: for (int i = 0; i < AVERAGE; i++) {
0874: start("GCal", "setGetHour");
0875: for (int j = 0; j < COUNT; j++) {
0876: dt.set(GregorianCalendar.HOUR_OF_DAY, 13);
0877: int val = dt.get(GregorianCalendar.HOUR_OF_DAY);
0878: if (dt == null) {
0879: System.out.println("Anti optimise");
0880: }
0881: }
0882: end(COUNT);
0883: }
0884: }
0885:
0886: private void checkDateSetGetHour() {
0887: int COUNT = COUNT_FAST;
0888: Date dt = new Date();
0889: for (int i = 0; i < AVERAGE; i++) {
0890: start("Date", "setGetHour");
0891: for (int j = 0; j < COUNT; j++) {
0892: dt.setHours(13);
0893: int val = dt.getHours();
0894: if (dt == null) {
0895: System.out.println("Anti optimise");
0896: }
0897: }
0898: end(COUNT);
0899: }
0900: }
0901:
0902: // To formatted string
0903: //------------------------------------------------------------------------
0904:
0905: private void checkJodaToString() {
0906: int COUNT = COUNT_SLOW;
0907: DateTime dt = new DateTime(GJChronology.getInstance());
0908: DateTimeFormatter f = DateTimeFormat.forPattern("dd MMM yyyy");
0909: for (int i = 0; i < AVERAGE; i++) {
0910: start("Joda", "toString");
0911: for (int j = 0; j < COUNT; j++) {
0912: String str = dt.toString("dd MMM yyyy");
0913: // String str = dt.toString(f);
0914: if (str == null) {
0915: System.out.println("Anti optimise");
0916: }
0917: }
0918: end(COUNT);
0919: }
0920: }
0921:
0922: private void checkJISOToString() {
0923: int COUNT = COUNT_SLOW;
0924: DateTime dt = new DateTime();
0925: DateTimeFormatter f = DateTimeFormat.forPattern("dd MMM yyyy");
0926: for (int i = 0; i < AVERAGE; i++) {
0927: start("JISO", "toString");
0928: for (int j = 0; j < COUNT; j++) {
0929: String str = dt.toString("dd MMM yyyy");
0930: // String str = dt.toString(f);
0931: if (str == null) {
0932: System.out.println("Anti optimise");
0933: }
0934: }
0935: end(COUNT);
0936: }
0937: }
0938:
0939: private void checkGCalToString() {
0940: int COUNT = COUNT_SLOW;
0941: GregorianCalendar dt = new GregorianCalendar();
0942: for (int i = 0; i < AVERAGE; i++) {
0943: start("GCal", "toString");
0944: for (int j = 0; j < COUNT; j++) {
0945: SimpleDateFormat sdf = new SimpleDateFormat(
0946: "dd MMM yyyy");
0947: String str = sdf.format(dt.getTime());
0948: if (str == null) {
0949: System.out.println("Anti optimise");
0950: }
0951: }
0952: end(COUNT);
0953: }
0954: }
0955:
0956: private void checkDateToString() {
0957: int COUNT = COUNT_SLOW;
0958: Date dt = new Date();
0959: for (int i = 0; i < AVERAGE; i++) {
0960: start("Date", "toString");
0961: for (int j = 0; j < COUNT; j++) {
0962: SimpleDateFormat sdf = new SimpleDateFormat(
0963: "dd MMM yyyy");
0964: String str = sdf.format(dt);
0965: if (str == null) {
0966: System.out.println("Anti optimise");
0967: }
0968: }
0969: end(COUNT);
0970: }
0971: }
0972:
0973: //------------------------------------------------------------------------
0974:
0975: /**
0976: * Start the stopwatch.
0977: */
0978: private void start(String str1, String str2) {
0979: result = (Result) results.get(str1 + str2);
0980: if (result == null) {
0981: result = new Result();
0982: result.object = str1;
0983: result.name = str2;
0984: results.put(str1 + str2, result);
0985: resultList.add(result);
0986: }
0987: start = System.currentTimeMillis();
0988: }
0989:
0990: /**
0991: * End the stopwatch and print the result.
0992: */
0993: private void end(int count) {
0994: end = System.currentTimeMillis();
0995: long time = (end - start);
0996: result.time = result.time + time;
0997: result.runs = result.runs + count;
0998: result.avg = (result.time * 1000000) / result.runs;
0999: System.out.print(".");
1000: }
1001:
1002: }
|