1

For a particular program of c code, I'd like to measure:

execution time (most likely complete program execution time)
memory usage
CPU load

how can i get all above information in C ?

pavan
  • 77
  • 10
  • The answer will likely vary based on the environment. For example, you might add a tag indicating the OS of interest (e.g., windows, linux, osx, etc.) – Mark Wilkins Apr 10 '13 at 14:35
  • If it's a command line tool in a UNIX based system you could use `time` for execution time measurement. In C, I personally use gettimeofday() before and after a code block. – guitarflow Apr 10 '13 at 14:37
  • This will help: http://stackoverflow.com/questions/5248915/execution-time-of-c-program – J.C.Morris Apr 10 '13 at 14:38
  • 1
    If you're looking for bottlenecks in your own programs, I suggest using a profiler. – autistic Apr 10 '13 at 14:40
  • platform is unix.. but i would like to know about windows too – pavan Apr 10 '13 at 14:43

1 Answers1

0

You can write a simple code to measure the execution time of a particular code block using the standard C library for time/date manipulation (time.h): http://en.wikipedia.org/wiki/C_date_and_time_functions

Memory usage is practically impossible to measure accurately for a non-trivial program. Does your program use shared libraries? Well then, do you count the memory used by shared libraries as belonging exclusively to your program? What about child processes that your program fork()ed? Do you count them separately or together with the main process? All of these questions can be answered, but that means that what you measure depends on your specific choices.

In C, what you can do is to wrap around malloc() and free() use your wrapper to keep track of your memory allocations.

For CPU load, you need to interface with the OS. You cannot get that information from C.