提交 ad575982 编写于 作者: R Rich Felker

fix clock() function

it previously was returning the pseudo-monotonic-realtime clock
returned by times() rather than process cputime. it also violated C
namespace by pulling in times().

we now use clock_gettime() if available because times() has
ridiculously bad resolution. still provide a fallback for ancient
kernels without clock_gettime.
上级 4054a135
#include <time.h> #include <time.h>
#include <sys/times.h> #include <sys/times.h>
/* this function assumes 100 hz linux and corrects for it */ int __clock_gettime(clockid_t, struct timespec *);
clock_t clock() clock_t clock()
{ {
struct timespec ts;
struct tms tms; struct tms tms;
return (unsigned long)times(&tms)*10000; if (!__clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts))
return ts.tv_sec*1000000 + ts.tv_nsec/1000;
__syscall(SYS_times, &tms);
return (tms.tms_utime + tms.tms_stime)*100;
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册