W
www
Hi,
I am using Eclipse profiler and have found that the following method,
getDate(), in a utility class was called more than 3000 times and cost 5
seconds of accumulative time.
public class Helper //a utility class
{
...//other helping methods
//get the String and return an object of Date
public static Date getDate(String str)
{
Date aDate = null;
if (timeString != null)
{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
try
{
aDate = format.parse(str);
}
catch ... //omitted
}
return aDate;
}
}
I am not very good with code performance. I feel at least something
below should be better:
public class Helper
{
...//other helping methods
private static Date aDate = null;
private static SimpleDateFormat format = (new
SimpleDateFormat("yyyy-MM-dd
HH:mm:ss")).setTimeZone(TimeZone.getTimeZone("UTC"));
public static Date getDate(String str) //now the method is much shorter
{
if (timeString != null)
{
try
{
aDate = format.parse(str);
}
catch ... //omitted
}
return aDate;
}
}
Do you think my new code is legal? Is better?
I am using Eclipse profiler and have found that the following method,
getDate(), in a utility class was called more than 3000 times and cost 5
seconds of accumulative time.
public class Helper //a utility class
{
...//other helping methods
//get the String and return an object of Date
public static Date getDate(String str)
{
Date aDate = null;
if (timeString != null)
{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
try
{
aDate = format.parse(str);
}
catch ... //omitted
}
return aDate;
}
}
I am not very good with code performance. I feel at least something
below should be better:
public class Helper
{
...//other helping methods
private static Date aDate = null;
private static SimpleDateFormat format = (new
SimpleDateFormat("yyyy-MM-dd
HH:mm:ss")).setTimeZone(TimeZone.getTimeZone("UTC"));
public static Date getDate(String str) //now the method is much shorter
{
if (timeString != null)
{
try
{
aDate = format.parse(str);
}
catch ... //omitted
}
return aDate;
}
}
Do you think my new code is legal? Is better?