?
Solved

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

Posted on 2006-06-27
3
Medium Priority
?
232 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 800 total points
ID: 16996866
0
 
LVL 28

Accepted Solution

by:
rrz earned 1200 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month8 days, 6 hours left to enroll

766 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