dimanche 21 juillet 2019

Impact of the prior loop iteration on the execution time of the current iteration

I am trying to perform a series of measurements to create a benchmark. A simplified version of what I have is brought here:

int performComputation(input) {

  // A. Allocate ~1GB memory here

  auto start = std::chrono::steady_clock::now();

  // B. Launch $nproc threads to run the program.

  auto end = std::chrono::steady_clock::now();
  float time = std::chrono::duration<double, std::milli>(end - start).count();

}

int main() {
  for (input: inputs)
    performComputation(input);
}

The problem is that using this loop in main, suddenly increases the measured time in the performComputation function. That is, if I call the function directly from main without any loop (as shown in what follows), the measure time is suddenly 100X smaller.

int main() {
  performComputation(input); // ~ 100X faster now that the loop is gone.
}

Possible Reasons

  1. I allocate a huge amount of memory in step A. I belive when I run the code in a loop, while the second iteration of loop being executed the computer is busy freeing the memory for the first iteration. Hence, the program becomes much slower. If this is the case, I'd be grateful if someone can suggest a change that I can get the same results with loop.

More Details

All the memory allocations are performed using std::vector.

Aucun commentaire:

Enregistrer un commentaire