Solved

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

Posted on 2006-06-27
3
226 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

910 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now