0

I need to write a program that return the time and amount of memory used by a specific function and prints it on the console, I already know how to measure the time used by the function, but I don't know how to do the same for the memory used.

I would like to get a value to be printed on the console so I cannot use external monitors. Currently I can not use another OS so this must be done on windows.

I looked online but I didn't find nor understand how can I do this.

Thank you.

Santiago
  • 3
  • 7
  • Are you referring to heap memory, stack memory, or code-page memory? – Jeremy Friesner Jun 16 '20 at 00:19
  • Sorry I am a beginner in programming, this was an assignment from my class, I guessed that it was ram memory so i am not sure what those types of memory are. – Santiago Jun 16 '20 at 00:34
  • I asked and the answer I got is that the memory to be displayed is virtual memory used. – Santiago Jun 16 '20 at 00:39
  • Does the memory-measurement have to work for *any function* or just for the one particular function you are expected to write? (I ask because if it only needs to work for your one function, then you can simply add code to your function to increase a memory_used variable by the appropriate amount every time you allocate some memory. If OTOH it needs to work for any possible function, that's a lot more difficult to do correctly, and probably outside the scope of a beginning programming class) – Jeremy Friesner Jun 16 '20 at 00:51
  • 2
    thats a tough thing to answer, even for experienced windows programmers. So i suspect that problem is simpler than you describe it. Cant believe I am typing this, but -> what does the actual assignment say? – pm100 Jun 16 '20 at 01:14
  • I have to get the running time and memory usage for several algorithms such as search and sorting algorithms and frequent problems algorithms (fibonacci number, n fact...) after that I just have to print the time and the memory used for that algorithm, I know how to do it for the time but I have no idea how to do it for the memory. – Santiago Jun 16 '20 at 02:05
  • Still not clear whether the Fibonacci, factorial, etc. functions are functions you wrote, or functions someone else wrote. If you wrote them (or you can modify them), then you can add instrumentation to track how much memory is used. – Raymond Chen Jun 16 '20 at 02:08
  • Although I'm a beginner, this is actually for an algorithm course, I am supposed to compare the results using tables and graphs, but doing the code part is also part of the assignment and I can only use windows. – Santiago Jun 16 '20 at 02:08
  • Well for the function itself it does not really matter if I myself write it or if it's taken from internet, for some functions like fibonacci I just wrote it myself since it's easy to remember but for the other ones I took them from internet, I can modified it if it's necessary since I just have to compare the performance of the functions. – Santiago Jun 16 '20 at 02:10
  • For tracking heap usage, see this link: https://stackoverflow.com/questions/910172/track-c-memory-allocations -- for tracking stack usage, you could call the `GetCurrentStackSize()` function (shown here: https://stackoverflow.com/a/1579386/131930 ) at various points during your function's execution and store the maximum value it returns during the function's run. – Jeremy Friesner Jun 18 '20 at 04:49

0 Answers0