简体   繁体   中英

Java local variable vs global field - Performance

I have a private method which takes date in as String type and returns XMLGregorianCalendar object with specific format. This method is being invoked from various other methods within the same class. The format defined as SimpleDateFormat which is same for every invocation. Below are the 2 versions of the method.

Version 1:

private XMLGregorianCalendar getXmlGregorianCalendar(final String strDt) throws ParseException, DatatypeConfigurationException{
        Date date = null;
        // local variable dtFormat
        DateFormat dtFormat = new SimpleDateFormat("yyyy-MM-dd-hh.mm.ss");
        if (strDt != null) {
            date = dtFormat.parse(strDt);
            GregorianCalendar cal = new GregorianCalendar();
            cal.setTime(date);
            XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
            return xmlCal;
       }else {
           return DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar());
       }
    }

Version 2:

// declare date format once, global variable
private DateFormat dtFormat = new SimpleDateFormat("yyyy-MM-dd-hh.mm.ss");

// getter setter for dtFormat

private XMLGregorianCalendar getXmlGregorianCalendar(final String strDt) throws ParseException, DatatypeConfigurationException{
        Date date = null;
        if (strDt != null) {
            date = this.dtFormat.parse(strDt); // of course, getDtFormat() can be used
            GregorianCalendar cal = new GregorianCalendar();
            cal.setTime(date);
            XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
            return xmlCal;
       }else {
           return DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar());
       }
    }

Version 1 vs Version 2. Which has better performance?

Note: The enclosed class is annotated as @Component and involved in creating response for my web service.

SimpleDateFormat is not thread-safe, so that would be something to consider. Performance has little to do with this question, unless you really don't have any other performance hotspots (which would be surprising).

You can however use a ThreadLocal SimpleDateFormat , but remember that ThreadLocals have their own issues, and it probably doesn't really matter.

If that SimpleDateFormat is a constant, you can make it static final and it would improve your performance just a little bit. But you should be aware of the concept of static fields in classes. static fields are allocated once in memory for all instances of your @Component class. So if this cannot creating a bug in your code, it's better to define it as a constant using static final keywords.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM