so I am building an html table using a list of object arrays, but its going very unacceptably slow. any way to optimize it?
public String generateHtmlTable(List<Object[]> resultSet) {
totalRows = resultSet.size();
StringBuilder sb = new StringBuilder();
sb.append("<table width=\"900px\">");
sb.append("<tr>");
sb.append("<th width=\"10%\" align=\"left\"><b>col1</b></th>");
sb.append("<th width=\"25%\" align=\"left\"><b>col2</b></th>");
sb.append("<th width=\"20%\" align=\"left\"><b>col3</b></th>");
sb.append("<th width=\"15%\" align=\"left\"><b>col4</b></th>");
sb.append("<th width=\"10%\" align=\"left\"><b>col5</b></th>");
sb.append("<th width=\"10%\" align=\"left\"><b>col6</b></th>");
sb.append("<th width=\"10%\" align=\"left\"><b>col7</b></th>");
sb.append("<th width=\"5%\" align=\"left\"><b>col8</b></th>");
sb.append("<th width=\"5%\" align=\"left\"><b>col9</b></th>");
sb.append("</tr>");
for (Object[] row : resultSet) {
sb.append("<tr>");
for (Object cell : row) {
sb.append("<td>");
sb.append(((cell != null) ? cell.toString() : ""));
sb.append("</td>");
}
sb.append("</tr>");
rowsProcessed += 1;
}
sb.append("</table>");
return sb.toString();
}
resultSet. Run your code with the profiler to see what really takes the most of processing time.append(). Something likesb.append("<th>...</th>" + "<th> ... </th>" + "<th> ... </th>");Is that going to help? You would have to try, I guess. Even better would be to omit the+operator as well, if possible.+. But if your code allows, you could try to omit that entirely and just append all cells in one call. That might help.try is constructing theStringBuilder with a given capacity, so it doesn't need to copy the internal data more often then it need to.