当前位置: 技术问答>linux和unix
times函数计时为0?
来源: 互联网 发布时间:2016-06-02
本文导语: #include #include #include #include int main(void) { double clockticks, cticks; clock_t tcend, tcstart; struct ...
#include
#include
#include
#include
int main(void) {
double clockticks, cticks;
clock_t tcend, tcstart;
struct tms tmend, tmstart;
if ((clockticks = (double) sysconf(_SC_CLK_TCK)) == -1) {
perror("Failed to determine clock ticks per second");
return 1;
}
printf("The number of ticks per second is %fn", clockticks);
if (clockticks == 0) {
fprintf(stderr, "The number of ticks per second is invalidn");
return 1;
}
if ((tcstart = times(&tmstart)) == -1) {
perror("Failed to get start time");
return 1;
}
sleep(2);
if ((tcend = times(&tmend)) == -1) {
perror("Failed to get end times");
return 1;
}
cticks = tmend.tms_utime + tmend.tms_stime
- tmstart.tms_utime - tmstart.tms_stime;
printf("Total CPU time for operation is %f secondsn",cticks/clockticks);
if ((tcend
#include
#include
#include
int main(void) {
double clockticks, cticks;
clock_t tcend, tcstart;
struct tms tmend, tmstart;
if ((clockticks = (double) sysconf(_SC_CLK_TCK)) == -1) {
perror("Failed to determine clock ticks per second");
return 1;
}
printf("The number of ticks per second is %fn", clockticks);
if (clockticks == 0) {
fprintf(stderr, "The number of ticks per second is invalidn");
return 1;
}
if ((tcstart = times(&tmstart)) == -1) {
perror("Failed to get start time");
return 1;
}
sleep(2);
if ((tcend = times(&tmend)) == -1) {
perror("Failed to get end times");
return 1;
}
cticks = tmend.tms_utime + tmend.tms_stime
- tmstart.tms_utime - tmstart.tms_stime;
printf("Total CPU time for operation is %f secondsn",cticks/clockticks);
if ((tcend