简单的程序可以通过命令实现
# time ./test real 0m2.033s user 0m2.032s sys 0m0.000s
不方便用time命令的可以使用系统函数实现
#include <sys/times.h> //用户获取用户时间,系统时间 static struct tms tms_start; static struct tms tms_end; //用于获取挂钟时间 static clock_t c_start; static clock_t c_end; void debug_times_start() { memset(&tms_start, 0, sizeof(struct tms)); memset(&tms_end, 0, sizeof(struct tms)); c_start = times(&tms_start); } void debug_times_end() { c_end = times(&tms_end); printf("clock time:%ld, user/system cpu time:%ld/%ld ", c_end - c_start, tms_end.tms_utime - tms_start.tms_utime, tms_end.tms_stime - tms_start.tms_stime); }