0

I am trying to write a code for reading 120 files from a folder and performing some calculations on it. When i debug the code, it works fine, however, execution time is more than 20 mins, I am aware that this might be due to bug in the code. However, can someone look into it and suggest possible methods to reduce the execution time. Kindly let me know if I should provide further information. Thank You.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

public class myclass {
  static int total = 1;
  static int r = 0;

  public static void main(String[] args) {

    ArrayList<Double> mysignal = new ArrayList<Double>();
    ArrayList<Double> mylist = new ArrayList<Double>();

    double x;
    double a;
    myclass obj = new myclass();
    String target_dir = "path for folder";
    File dir = new File(target_dir);
    File[] files = dir.listFiles();

    for (File f : files) {
      if (f.isFile()) {
        BufferedReader inputStream = null;

        try {
          inputStream = new BufferedReader(new FileReader(f));
          String line;

          while ((line = inputStream.readLine()) != null) {
            System.out.println(line);
            mysignal.add(Double.valueOf(line));
            total++;

          }
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }

        a = obj.funtioneg(mysignal, total);
        mylist.add(r, a);
        System.out.println(mylist.get(r));
        r++;

      }
    }
  }

  public double functioneg(ArrayList<Double> s, int N) {

    ArrayList<Double> y = new ArrayList<Double>();
    double sum = 0, a1 = 0;
    double[] o1 = new double[N - 1];// processed signal

    for (int n = 0; n < counter_main - 1; n++) {
      for (int k = 0; k < 40; k++) {

        if (n - k >= 0) {
          a1 = s.get(n - k);
          sum = sum + (a1 * a1);// energy

        } else
          sum = sum + 0;
      }

      o1[n] = sum;

      sum = 0;

    }
    double sum1 = 0;
    double avg;
    for (int t = 0; t < counter_main - 1; t++) {

      sum1 = sum1 + o1[t];
    }

    avg = sum1 / N - 1;

    return (avg);
  }
}
sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • How many files do you try to parse and how big are those files? What happens if you try to do this with just a few files. Is it slow still? – idipous Dec 03 '15 at 19:49
  • 4
    I think this question might be more appropriate for the Code Review site, http://codereview.stackexchange.com/ – Erick G. Hagstrom Dec 03 '15 at 19:49
  • @idipous, 120 files, size of each ~14KB – user3602066 Dec 03 '15 at 20:03
  • Ask on Code Review, and when you do, please explain what you are trying to do so that we don't have to reverse-engineer your code: what the input looks like, and what calculations you are performing. – 200_success Dec 03 '15 at 20:07
  • @200_success Thank you, posted it on code review as well, actually I am struggling with this past few days, I have to further write the calculated data in a txt file. – user3602066 Dec 03 '15 at 21:30

3 Answers3

1

You need to close your InputStream

After reading each file in the directory (after your try - catch block) write the statement:

inputStream.close();
andrewdleach
  • 2,458
  • 2
  • 17
  • 25
0

As andrewdleach pointed out, you should close your input stream.

Additionally you might want to try out the Java 8 function Files#walk (see this question) for more efficiently walking through the files.

Community
  • 1
  • 1
Niklas
  • 375
  • 1
  • 3
  • 17
0

First try to comment out the line:

System.out.println(line);

The output to console is slow (and I mean really slow), this line is basically duplicating the contents of each processed file to the console.

Other than that, you can as well try to accumulate time spent in the functioneq() method and/or parts of it (for example using System.nanoTime()) to find the most time consuming parts (or run under debugger and use profiling by sampling, which is the easiest profiling method and surprisingly effective - just pause the program repeatedly and see where it paused most frequently).

EmDroid
  • 5,918
  • 18
  • 18
  • Thank you for the reply. I removed the print statement, however, it is still taking time. Actually, each file contains around 2000 numbers which are getting processed in the functioneg. – user3602066 Dec 03 '15 at 21:24
  • Btw. what is the (usual) value of the variable counter_main? I don't see it declared/set in the code posted ... – EmDroid Dec 04 '15 at 08:08