2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018

04/22/2004: POI Optimization - Speeding up org.apache.poi.hssf.usermodel.HSSFRow.compareTo()

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;
    }


subscribe via RSS