C ++ code runs slower over time

I have a long C ++ program made up of thousands of lines of code, several classes, functions, etc. The program basically reads information from the input file, runs the algorithm, and writes the results to the output files.

Today I realized that the running time of the program changes dramatically from time to time. So I do a little test, restarting my computer, shutting down everything possible, and running the code 5 times in a row using the same input file. The execution time is 50, 80, 130, 180, 190 seconds, respectively.

My first guess in this situation is unused dynamic memories. But I only use dynamic arrays twice in the whole code, and I am sure that I am deleting those arrays.

Do you guys have an explanation? I am using Visual Studio 2010 on a Windows 7 machine.

+3


source to share


2 answers


Beware of running programs from the visual studio debugger as this disables the LFH (low fragmentation) memory allocator. Try software outside of VS.

I've seen cases where it would take a few seconds for tasks to complete, usually hours to complete, just by starting visual studio from within.



First of all, if you still don't know what is going on, divide and win. Attach an app to see the subsystem battery life, or simply set debug timers in different areas to see where the runtime changes dramatically and unfolds from there. If it is a memory allocator problem, you usually see long gaps when freeing arrays.

+3


source


Your code runs in an environment that includes the state of the operating system, disk, network, time, memory, other running processes, etc.

Executing the same code in the same environment will give the same result every time.

Now you get different results (runtime). If you reuse the same executable file, then something changes in the environment.



Now the most obvious question is, is your code causing the environment to change? A simple example would be: it reads in a file, changes data, and writes it back to the same file.

You know your code. Just use this approach to isolate any effects your code might have in your environment and you will find the reason.

+2


source







All Articles