-1
\$\begingroup\$

I have a program that reads a .txt file from a given directory. Each record line contains a data and I am parsing it. I think this is costly with it comes to performance but I might be wrong. Here is the method. How can I improve it to improve performance?

currentLine = uploadFileReader.readLine();

while (!isBlankOrNull(currentLine)) {                 
            line++;
            String[] record = parseRecord(currentLine);
     // process the record array here..
}
private String[] parseRecord(String currentLine){

    String[] recordLine = currentLine.split("\\|");
    String[] record = null;
    String[] zeroArray = null;          

    if(recordLine.length == 4){
        zeroArray = new String[] {"0","0","0","0","0","0"};
        record = (String[]) ArrayUtils.addAll(recordLine, zeroArray);
    }else if(recordLine.length == 5){
        zeroArray = new String[] {"0","0","0","0","0"};
        record = (String[]) ArrayUtils.addAll(recordLine, zeroArray);  
    }else if(recordLine.length == 6){
        zeroArray = new String[] {"0","0","0","0"};
        record = (String[]) ArrayUtils.addAll(recordLine, zeroArray);  
    }else if(recordLine.length == 7){
        zeroArray = new String[] {"0","0","0"};
        record = (String[]) ArrayUtils.addAll(recordLine, zeroArray);  
    }else if(recordLine.length == 8){
        zeroArray = new String[] {"0","0"};
        record = (String[]) ArrayUtils.addAll(recordLine, zeroArray);  
    }else if(recordLine.length == 9){
        zeroArray = new String[] {"0"};
        record = (String[]) ArrayUtils.addAll(recordLine, zeroArray);  
    }else{
        record = recordLine;
    }

    return record;
}
\$\endgroup\$

closed as off-topic by 200_success Nov 11 '15 at 7:40

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – 200_success
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Is that while loop right? Either fix it or omit it from the question. \$\endgroup\$ – 200_success Nov 11 '15 at 7:32
  • \$\begingroup\$ Cross-posted from Stack Overflow — please declare cross-postings. \$\endgroup\$ – 200_success Nov 11 '15 at 7:34
2
\$\begingroup\$

You can rewrite this more simply as follows. The requirement appears to be return an array of String always length 10 with "0" filling any unused cells?

Without real test data I cannot comment on its relative performance to your original, it does however significantly "less" work, less array allocations, condition checking etc, so is "very likely" to be faster...

private String[] parseRecord(String currentLine) {
    String[] recordLine = currentLine.split("\\|");
    String[] record = new String[10];
    for (int i = 0; i < record.length; i++) {
        if (i < recordLine.length) {
            record[i] = recordLine[i];
        } else {
            record[i] = "0";
        }
    }
    return record;
}

Test

public static void main(String[] args) {
    System.out.println(Arrays.toString(parseRecord("1")));
    System.out.println(Arrays.toString(parseRecord("1|2")));
    System.out.println(Arrays.toString(parseRecord("1|2|3|4|5|6|7|8|9")));
    System.out.println(Arrays.toString(parseRecord("1|2|3|4|5|6|7|8|9|10")));
}

Output

[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 2, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.