nLee
nLee

Reputation: 1385

Which is a more efficient way to write this block of code

I am writing a module that will generate a DDL based on certain input files. I am trying to figure out which way is more efficient.

This:

for (int i = 0; i < Common.testFields.size(); i++) {
    line = "";
    line += "  " + Common.testFields.get(i).toString() + "  ";
    line += Common.dataTypes.get(i).toString(); 
    if (i != Common.testFields.size() - 1) 
        line += ",";
    ddl += line;
    if (i != Common.testFields.size() - 1) 
        ddl += "\n";
}

or this:

for (int i = 0; i < Common.testFields.size(); i++) {
    line = "";
    line += "  " + Common.testFields.get(i).toString() + "  ";
    if (i == Common.testFields.size() - 1) {
        line += Common.dataTypes.get(i).toString(); 
        ddl += line;
    }
    else {
        line += Common.dataTypes.get(i).toString() + ",";
        ddl += line + "\n";
    }
}

I timed the execution for both for loops using System.currentTimeMillis(), but when I print out the elapsed time, it prints out 1 millisecond for both. Common.testFields.size() is currently 165. Considering the fact that both of these have the same complexity of O(n), If Common.testFields.size() were considerably larger, say 1000 or even 10000 times larger than it is currently, which one of these,if any, would be faster?

Upvotes: 0

Views: 113

Answers (3)

BeyelerStudios
BeyelerStudios

Reputation: 4283

If you want to be sure, you'll have to measure.

// the JIT might be able to do this optimisation, but lets be sure
StringBuilder builder = new StringBuilder();

// resolve this just once (assuming it stays the same during your loop), you never know how the VM is going to do this lookup
// also change type of list to the type of Common.testFields
ArrayList<? extends Object> fields = Common.testFields;
ArrayList<? extends Object> data = Common.dataTypes;
for (int i = 0, size = fields.size(); i < size; i++) {
    builder.append("  ").append(fields.get(i)).append("  ").append(data.get(i));
    if (i != size - 1)
        builder.append(",\n");
}
ddl = builder.toString(); // or += or whatever the previous state of ddl is...

Upvotes: 0

Daniel
Daniel

Reputation: 4549

My favorite "join" pattern is this:

StringBuilder builder = new StringBuilder();
String separator = "";

for (int i = 0; i < Common.testFields.size(); i++) {
    builder.append(separator)
           .append("  ")
           .append(Common.testFields.get(i))
           .append("  ")
           .append(Common.dataTypes.get(i));

    separator = ",\n";
}

String ddl = builder.append("\n").toString();

No special case "if last" or "if first" necessary.

Upvotes: 4

Danny Rancher
Danny Rancher

Reputation: 2005

How about this...

//assuming
String ddl = new String("")

for (int i = 0; i < Common.testFields.size(); i++){
    ddl += "  "
         + Common.testFields.get(i).toString()
         + "  "
         + Common.dataTypes.get(i).toString()
         + ",\n";
}

ddl = ddl.substring(0, ddl.length()-3);
ddl += "/n";

Upvotes: 1

Related Questions