The compareTo() method calls getRowNum() up to three times. Normally this isn’t a problem however the compareTo() method is called a lot - over three million times in my test case which uses a small Excel spreadsheet.

Using a local int to cache the values reduced the exection time by 26.3 seconds. Here is the new version of compareTo():

    public int compareTo(Object obj) {
        HSSFRow loc = (HSSFRow) obj;
        int rv = -1;
        int thisRowNum = this.getRowNum();
        int locRowNum = loc.getRowNum();

        if (thisRowNum == locRowNum) {
            rv = 0;
        } else if (thisRowNum < locRowNum) {
            rv = -1;
        } if (thisRowNum > locRowNum) {
            rv = 1;
        }
        return rv;
    }