I have a program to read signal data from 120 files in a folder and performing the energy of the signal.

It works correctly, but execution time is more than 20 mins, so there may be a problem with the code. Can someone look into it and suggest possible methods to reduce the execution time?

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) {


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

        a = obj.funtioneg(mysignal, total);
        mylist.add(r, a);


  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 < N - 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 < N- 1; t++) {

      sum1 = sum1 + o1[t];

    avg = sum1 / N - 1;

    return (avg);
  • \$\begingroup\$ How are the files formatted? Can you change the format of them? \$\endgroup\$
    – Emz
    Dec 3, 2015 at 21:17
  • 1
    \$\begingroup\$ What value(s) are you computing? What is the purpose of your code? \$\endgroup\$
    – Marvin
    Dec 3, 2015 at 21:21
  • \$\begingroup\$ Cross-posted from Stack Overflow under a different account. \$\endgroup\$ Dec 3, 2015 at 21:32
  • 1
    \$\begingroup\$ To bring this question up to Code Review standards, please see How to Ask. \$\endgroup\$ Dec 3, 2015 at 21:32
  • \$\begingroup\$ Thanks a lot vnp and 200_success, I was appending the previous data along with the current data and corrected the same. Thank you for the detailed feedback! \$\endgroup\$ Dec 4, 2015 at 0:20

2 Answers 2

  • Avoid recomputations

    You compute the energy accumulated in a sliding window. When a window slides by 1, one sample shifts out, and one sample shifts in. The accumulated energy changes by s[n]^2 - s[n-40]^2. Therefore the inner loop is superficial. This alone will give you a 40-fold improvement.

  • Quadratic complexity

    mysignal is only getting more signals appended to it, that is its initial parts are fed to functioneg over and over again, mostly producing the same result. In fact you only need 40 last of the processed samples to ramp up the new file.

  • Avoid reallocations

    You don't really need the o1 array. sum1 can be computed on the fly.


mysignal never gets cleared between files; thus it accumulates the data from all previously processed files. I assume that it is not your intention to process the earlier chunks of data again and again.

You could fix that bug, but the root cause is that you aren't splitting work into functions. Instead of a big for loop in main, if you had broken down the work as

for (File f : files) {
    double energy = calculateEnergy(f);

… you wouldn't have introduced that kind of problem.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.