Measuring the memory usage and execution time of any Windows executable

时间:2017-08-13 13:52:46

标签: c++ windows batch-file

I need to measure memory usage and execution time of any Windows executable file by creating some batch file or code snippet. And just like the online judges, I want the process to be fully automated, so I don't have to open Task Manager every time I need to measure, or inserting additional lines into the original source files.

For example, if I compile this particular C++ source to an executable a.exe:

#include <cstdio>

int main()
{
    printf("%d", 1 + 2);
}

And measure the execution time and memory usage of this particular a.exe, the batch should produce something like:

Used: 0.015s, 3092K

I have researched for hours on StackOverflow, but the result is either for UNIX binaries, or code lines for embedding into the source.

Many thanks in advance.

1 个答案:

答案 0 :(得分:2)

Microsoft already has something that does that (GetProcessMemoryInfo). The example code that MS provides will list the memory usage of all running processes. However, you can change that by specifying your program's handle.

Documentation: https://msdn.microsoft.com/en-us/library/windows/desktop/ms683219(v=vs.85).aspx

Example code: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682050(v=vs.85).aspx

As for time, I stumbled upon a snippet from another question (Easily measure elapsed time)

#include <ctime>

void f() {
  using namespace std;
  clock_t begin = clock();

  //run a.exe

  clock_t end = clock();
  double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
}
相关问题