簡單的程序可以通過命令實現
[plain]
# time ./test
real 0m2.033s
user 0m2.032s
sys 0m0.000s
# time ./test
real 0m2.033s
user 0m2.032s
sys 0m0.000s
不方便用time命令的可以使用系統函數實現
[cpp]
#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\r\n",
c_end - c_start,
tms_end.tms_utime - tms_start.tms_utime,
tms_end.tms_stime - tms_start.tms_stime);
}
#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\r\n",
c_end - c_start,
tms_end.tms_utime - tms_start.tms_utime,
tms_end.tms_stime - tms_start.tms_stime);
}