Subject | Optimisation considerations |
---|---|
Author | Pazzo Pazzo |
Post date | 2002-07-17T09:34:47Z |
Browsing the CVS sources I noticed the use of patterns
which cause unnecessary object creations.
1. String and StringBuffer:
Not optimal code:
String sql = selectClause;
sql += whereClause;
sql += orderByClause;
...
this generates the following code:
String sql = selectClause;
sql = new
StringBuffer(sql).append(whereClause).toString();
sql =
StringBuffer(sql).append(orderByClause).toString();
...
to avoid the unnecessary StringBuffer creation and
enlargement (new char[] creation) a pattern like the
following should be used:
// The initial size of the internal char array should
// be specified as needed or better be calculated a
// priori, for example:
// int len = selectClause.length() +
whereClause.length() ...
StringBuffer sb = new StringBuffer(100);
sb.append(selectClause);
sb.append(whereClause);
sb.append(orderByClause);
String sql = sb.toString();
2. Another place where object creations could be
avoided are ArrayList creations.
Not optimal code:
ArrayList params = new ArrayList();
if (<condition1>)
params.add(<param1>);
if (<condition2>)
params.add(<param2>);
...
foo(..., params, ...);
...
Optimized code:
ArrayList params = null;
if (<condition1>) {
if (params == null)
params = new ArrayList();
params.add(<param1>);
}
if (<condition2>) {
if (params == null)
params = new ArrayList();
params.add(<param2>);
}
...
foo(..., params, ...);
...
Of course all methods have to accept null parameters
to avoid the need to create empty (dummy) objects.
3. Checking for empty strings:
Not ideal code:
if (!str.equals("")) {
...
}
Better:
if (str != null && str.length() > 0) {
...
}
Java is a high level language and a lot of things are
done behind the scenes. But considering these and
others while coding can make even a Java app
efficient.
Best regards
Patrick
__________________________________________________
Do You Yahoo!?
Yahoo! Autos - Get free new car price quotes
http://autos.yahoo.com
which cause unnecessary object creations.
1. String and StringBuffer:
Not optimal code:
String sql = selectClause;
sql += whereClause;
sql += orderByClause;
...
this generates the following code:
String sql = selectClause;
sql = new
StringBuffer(sql).append(whereClause).toString();
sql =
StringBuffer(sql).append(orderByClause).toString();
...
to avoid the unnecessary StringBuffer creation and
enlargement (new char[] creation) a pattern like the
following should be used:
// The initial size of the internal char array should
// be specified as needed or better be calculated a
// priori, for example:
// int len = selectClause.length() +
whereClause.length() ...
StringBuffer sb = new StringBuffer(100);
sb.append(selectClause);
sb.append(whereClause);
sb.append(orderByClause);
String sql = sb.toString();
2. Another place where object creations could be
avoided are ArrayList creations.
Not optimal code:
ArrayList params = new ArrayList();
if (<condition1>)
params.add(<param1>);
if (<condition2>)
params.add(<param2>);
...
foo(..., params, ...);
...
Optimized code:
ArrayList params = null;
if (<condition1>) {
if (params == null)
params = new ArrayList();
params.add(<param1>);
}
if (<condition2>) {
if (params == null)
params = new ArrayList();
params.add(<param2>);
}
...
foo(..., params, ...);
...
Of course all methods have to accept null parameters
to avoid the need to create empty (dummy) objects.
3. Checking for empty strings:
Not ideal code:
if (!str.equals("")) {
...
}
Better:
if (str != null && str.length() > 0) {
...
}
Java is a high level language and a lot of things are
done behind the scenes. But considering these and
others while coding can make even a Java app
efficient.
Best regards
Patrick
__________________________________________________
Do You Yahoo!?
Yahoo! Autos - Get free new car price quotes
http://autos.yahoo.com