2

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.

  • So you want your code to do something else but without altering it! Your solution then is to create some more code which runs along side it with the added functionality. That is your job to research and put together, we are here to help you with problems with your code not to design and create it for you. – Compo Aug 13 '17 at 15:33

1 Answers1

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;
}
KonaeAkira
  • 236
  • 1
  • 11
  • As I said, I don't want to insert lines into the original code – Đặng Đoàn Đức Trung Aug 13 '17 at 15:12
  • 1
    I think you are getting something wrong here. Both the examples i gave can be run independent from your program. The first one will check your memory usage when a.exe is already running. The second one will execute a.exe to measure it's runtime. How you combine them is up to you ;) – KonaeAkira Aug 13 '17 at 15:46