Solved

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

Posted on 2006-06-27
3
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

749 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