Reputation: 11
How can I calculate the time taken and the memory used by a certain program during run-time?
I am a beginner in C++. I have seen many answers and different codes for my question, but all of them I do not understand.. Is there a simple way to do this?
And if there is a more complicated way, please include explanation.
Thanks.
Upvotes: 1
Views: 3313
Reputation: 7400
If you are using windows, the built in Windows Task Manager will show you your memory usage in the "Processes" tab. Similarly, Mac OSX has the "Activity Monitor" which will show you your memory usage. The numbers in the program will change in real time as your program changes states, and allocates/deallocates memory.
As for measuring execution times, you can use a timer function to get the current system time before and after execution, then just do the math.
QueryPerformanceCounter and QueryPerformanceFrequency for windows, and gettimeofday on a mac/linux machine.
If you needed more specific performance related information about your application, a profiling app like AMD CodeAnalyst can help you find out where your major bottlenecks are.
Upvotes: 0
Reputation: 33607
There is not now (nor will there be) a generalized tool which can tell you how long an arbitrary program will take without actually running it, which in the general case could take forever. That's a fun CS concept to meditate upon:
http://en.wikipedia.org/wiki/Halting_problem
In short: the time your program will take to run can't be known in advance, unless you're operating under a fairly constrained and mathematically rigorous environment, and have stylized your code appropriately on a system offering certain guarantees:
http://en.wikipedia.org/wiki/Real-time_operating_system
If you're experienced, you can sorta-kinda estimate the scale of a memory footprint of a program by looking at the size of your data structures and how many of them there are. The C++ operator sizeof
can help you determine the concrete number of bytes consumed by any individual object, though it won't tell you anything about the amount of memory used for "bookkeeping" behind-the-scenes.
But once again, you mostly just have to run it and use a process monitor to see what happens. Hard to predict, you just empirically examine what happens in practice:
Tracking CPU and Memory usage per process (Windows)
monitor a program's memory usage in Linux
Upvotes: 2