My boss asks me to develop a simple Java application to sort xls file containing a list of keywords. Everything works fine, but for big xls files my app take very, very long time to do her job. I think that my algorithms aren't really optimized but I've seen that the execution of my jar doesn't impact at all the performances of my computer (Windows 7 Pro 32 bits of my boss), and in fact it uses really nothing on the CPU and memoty. So, here's my question, how do I allow my program to take every thing he needs in power and memory to run and sort that xls like a Lamborghini ? Thanks !
EDIT: Here is my code, divides in two block: a main and a frame class:
public class Frame extends JFrame implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
JTextField input;
JTextField output;
JTextField result;
JTextArea gomin;
JTextArea info;
Frame()
{
JFrame jfrm = new JFrame("App CRI");
jfrm.setLocation(100, 100);
jfrm.setLayout(new BorderLayout());
jfrm.setSize(400, 170);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setResizable(false);
input = new JTextField(10);
output = new JTextField(10);
result = new JTextField(10);
gomin = new JTextArea();
result.setEditable(false);
info = new JTextArea("Merci de renseigner le nom des fichiers sans extension.\nExemple: \"10-09-2014\" pour le fichier \"10-09-2014.xls\".");
info.setEditable(false);
// set input fields
JPanel inFieldPane = new JPanel();
inFieldPane.setLayout(new GridLayout(2,2));
inFieldPane.add(new JLabel("Input file name:"));
inFieldPane.add(input);
input.addActionListener(this);
inFieldPane.add(new JLabel("Output file name:"));
inFieldPane.add(output);
output.addActionListener(this);
jfrm.add(inFieldPane,BorderLayout.NORTH);
// set submit button
JPanel submitPane = new JPanel();
submitPane.setLayout(new FlowLayout());
submitPane.add(info);
JButton submitButton = new JButton("Let's go !");
submitButton.addActionListener(this);
submitPane.add(submitButton);
jfrm.add(submitPane,BorderLayout.CENTER);
// display results
JPanel outFieldPane= new JPanel();
outFieldPane.setSize(350, 50);
outFieldPane.add(gomin);
gomin.setSize(350, 50);
gomin.setEditable(false);
jfrm.add(outFieldPane,BorderLayout.SOUTH);
jfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Let's go !"))
{
gomin.setText(Main.doIt(input.getText().trim(), output.getText().trim()));
}
}
}
And here is my main class with all my methods, I haven't separated these methods in other classes even if I have too, and the quality isn't pretty bad I know:
public class Main {
public static ArrayList<String> loadKeywords(String src)
{
ArrayList<String> arr = new ArrayList<String>();
try
{
InputStream inp = new FileInputStream(src);
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row r;
for(int i = 1; i <= sheet.getLastRowNum(); i++)
{
r = sheet.getRow(i);
if(r.getCell(0) != null && r.getCell(0).getCellType() == Cell.CELL_TYPE_STRING)
arr.add(r.getCell(0).getRichStringCellValue().getString());
}
} catch (FileNotFoundException e)
{
System.out.println("Keywords file undetected !");
e.printStackTrace();
} catch (InvalidFormatException e)
{
System.out.println("Invalid format. Oups.");
e.printStackTrace();
} catch (IOException e)
{
System.out.println("Fuck ! IOException !");
e.printStackTrace();
}
return arr;
}
public static boolean isValidCri(String inputString, ArrayList<String> arr)
{
for(int i =0; i < arr.size(); i++)
{
if(Pattern.compile(Pattern.quote(arr.get(i)), Pattern.CASE_INSENSITIVE).matcher(inputString).find())
{
return true;
}
}
return false;
}
public static void process()
{
System.out.println("Process en cours ... ");
ArrayList<String> arr = loadKeywords("C:\\Users\\9108250x\\Desktop\\app_cri\\keywords.xls");
if(arr.isEmpty())
System.out.println("rat�, erreur au chargement des keywords :(");
else
{
try
{
InputStream inp = new FileInputStream("C:\\Users\\9108250x\\Desktop\\app_cri\\01-09-2014.xls");
Workbook wb = WorkbookFactory.create(inp);
Sheet itaSheet = wb.getSheetAt(0);
Row r;
int endy = itaSheet.getLastRowNum();
for(int i = 1; i <= endy; i++)
{
r = itaSheet.getRow(i);
if(r.getCell(27) != null && r.getCell(27).getCellType() == Cell.CELL_TYPE_STRING)
{
if(! isValidCri(r.getCell(27).getRichStringCellValue().getString(), arr))
{
itaSheet.shiftRows(i+1, itaSheet.getLastRowNum()+1, -1);
i--;
endy--;
}
}
else
{
itaSheet.shiftRows(i+1, itaSheet.getLastRowNum()+1, -1);
i--;
endy--;
}
}
FileOutputStream fileOut = new FileOutputStream("C:\\Users\\9108250x\\Desktop\\app_cri\\outPut.xls");
wb.write(fileOut);
fileOut.close();
}
catch (FileNotFoundException e)
{
System.out.println("File not found, bad url.");
e.printStackTrace();
}
catch (InvalidFormatException e)
{
System.out.println("Invalid file format.");
e.printStackTrace();
}
catch (IOException e)
{
System.out.println("Fuck, doesn't work !");
e.printStackTrace();
}
System.out.println("et termin� avec succ�s !");
}
}
public static String doIt(String input, String output)
{
String result = "succeed !";
ArrayList<String> arr = loadKeywords(".\\keywords.xls");
if(arr.isEmpty())
result = "no keywords";
else
{
try
{
InputStream inp = new FileInputStream(".\\"+ input +".xls");
Workbook wb = WorkbookFactory.create(inp);
Sheet itaSheet = wb.getSheetAt(0);
Row r;
int endy = itaSheet.getLastRowNum();
for(int i = 1; i <= endy; i++)
{
r = itaSheet.getRow(i);
if(r.getCell(27) != null && r.getCell(27).getCellType() == Cell.CELL_TYPE_STRING)
{
if(! isValidCri(r.getCell(27).getRichStringCellValue().getString(), arr))
{
itaSheet.shiftRows(i+1, itaSheet.getLastRowNum()+1, -1);
i--;
endy--;
}
}
else
{
itaSheet.shiftRows(i+1, itaSheet.getLastRowNum()+1, -1);
i--;
endy--;
}
}
FileOutputStream fileOut = new FileOutputStream(".\\"+ output +".xls");
wb.write(fileOut);
fileOut.close();
}
catch (FileNotFoundException e)
{
System.out.println("File not found, bad url.");
result = "file not found";
e.printStackTrace();
}
catch (InvalidFormatException e)
{
System.out.println("Invalid file format.");
result = "invalid operation";
e.printStackTrace();
}
catch (IOException e)
{
System.out.println("Doesn't work !");
result = "IO exception";
e.printStackTrace();
}
catch ( Exception e)
{
result = "unknow exception";
}
System.out.println("et termin� avec succ�s !");
}
return result;
}
/**
* @param args
*/
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new Frame();
}
});
// process();
}
}