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);
}
}