Solved

This code is slow, any suggestions for making it faster?

Posted on 2006-06-27
3
228 Views
Last Modified: 2010-03-31
I am creating a site using tag libraries in JSP.  The purpose of the tag in question is to list all of the files of a given type in a directory by date (newest first).

The way I am doing it is to put all the file info into a linked list, then use a list iterator to send values back to the page through the TagLibrary. I won't post the whole lot as its pretty big, but the part that is slowing it down is the method that attempts to put it all in order:

String type = ".doc";
String fileDir = "/home/mr_lenehan/public_html/uploads/";
File dir = new File(fileDir);
String[] files = dir.list();
SimpleDateFormat sdf = new SimpleDateFormat( "dd MMM yy" );
ListIterator validFiles;

public LinkedList linkedListOfValidTypeFilenames(){
            int typeCounter=0;      //count up how many files we're working with
            for(int i =0; i<files.length-1;i++){
                  if(isType(type,files[i])){
                        typeCounter++;            
                  }
            }
            //make two String arrays, one to hold names, the other timestamps
            String[] buildString = new String[typeCounter];
            String[] buildOrder = new String[typeCounter];
            typeCounter=0;
            //get the file names and dates
            for(int i =0; i<files.length-1;i++){
                  if(isType(type,files[i])){
                        //checks against the type we want
                        buildString[typeCounter]=files[i];
                        buildOrder[typeCounter]=""+new File(fileDir+files[i]).lastModified();
                        if(buildOrder[typeCounter].length()==12){
                              //if its less than 13 then add 0 to front as we use array sort later
                              buildOrder[typeCounter]="0"+new File(fileDir+files[i]).lastModified();
                        }
                        output(buildOrder[typeCounter]);
                        typeCounter++;
                        numFiles++;            
                  }
            }
            //sort the timestamps
            Arrays.sort(buildOrder);
            String[] ascOrderedStringArray = new String[typeCounter];
            //load a string array with the files in order
            //outer loop holds info on correct order, inner loop sorts out filenames
            for(int i = 0; i <typeCounter;i++){
                  for(int n = 0; n<typeCounter;n++){
                        File workingFile = new File(fileDir+buildString[n]);
                        if((""+workingFile.lastModified()).equals(buildOrder[i])){                              
                              ascOrderedStringArray[i]=workingFile.getName();
                        }
                  }
            }
            String[] decOrderedStringArray = new String[typeCounter];
            //reverse the order
            for(int i = 0;i<typeCounter;i++){
                  decOrderedStringArray[i]=ascOrderedStringArray[typeCounter-i-1];
            }
            if (checkRange()){
                  //somewhere else the user can specifiy files X to Y from the dir
                  //checkRange makes sure their request is reasonable
                  LinkedList fileList = new LinkedList();
                  for(int i=start;i<=finish;i++){
                        File workingFile = new File(fileDir+decOrderedStringArray[i]);
                        String[] entry = {workingFile.getName(),""+workingFile.length()/1024,""+sdf.format(new Date(workingFile.lastModified()))};
                        fileList.add(entry);
                  }
                  return fileList;
            }
            return new LinkedList();
      }

I'm quite certain that all the loops, checking and file accessing are what slowing it down.  On small dir's the code runs fine, on large dir's I'm waiting for 30-50 seconds.

Can anyone make a suggestion on a better way of performing this task?
0
Comment
Question by:Mr_Lenehan
  • 2
3 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16996860
Store your files as an array of FIles, and use Arrays.sort() with a custom Comparator that implments the required sort order.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 200 total points
ID: 16996866
0
 
LVL 27

Accepted Solution

by:
rrz earned 300 total points
ID: 16997406
objects has the right idea. You should look at some examples on this site.  Here is one.
http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_21861191.html 
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question