Solved

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

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array11 challenge 16 52
parentbit challenge 3 52
firstChar challenge 13 86
create a gui in perl 3 46
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 …
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

747 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

11 Experts available now in Live!

Get 1:1 Help Now