This may be tedious, but I would love to know which is a more efficient way of programming. The result may only be slightly better or slightly worse, but I am still interested in which is 'better'.
Say I want to create an array of String
objects based on the amount of columns I received from an SQL query and then go ahead and fill this String[]
object with values through the use of a for
loop. i.e.
if(rs.next()) {
String[] topLine = new String[metadata.getColumnCount()];
for (int i = 0; i < metadata.getColumnCount(); i++) {
topLine[i] = getColumnValue(rs, metadata.getColumnType(i + 1), i + 1);
}
writeNext(topLine);
}
See the use of metadata.getColumnCount()
to fetch the amount of columns, only twice. Would it be more appropriate (and faster) to bring this value into it's own variable such that:
int columnCount = metadata.getColumnCount();
and make use of it only twice? Or should I leave the method to call twice?
What n
times do I have to call metadata.getColumnCount()
before it becomes more efficient to use a local variable. Is there even any change?
Edit: By the way, this isn't my code, I picked it up as an example.
N = 1
.