简体   繁体   中英

Measuring C execution time, memory usage and cpu load for a program

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM