Linux下使用clock_gettime给程序计时
2012-01-12 18:32
645 查看
哦,clock_gettime( ) 提供了纳秒的精确度,给程序计时可是不错哦;
函数的原型如下:
int clock_gettime(clockid_t clk_id, struct timespect *tp);
clockid_t clk_id用于指定计时时钟的类型,对于我们Programmr以下三种比较常用:
CLOCK_REALTIME, a system-wide realtime clock.
CLOCK_PROCESS_CPUTIME_ID, high-resolution timer provided by the CPU for each process.
CLOCK_THREAD_CPUTIME_ID, high-resolution timer provided by the CPU for each of the threads.
CLOCK_REALTIME, a system-wide realtime clock.
CLOCK_PROCESS_CPUTIME_ID, high-resolution timer provided by the CPU for each process.
CLOCK_THREAD_CPUTIME_ID, high-resolution timer provided by the CPU for each of the threads.
struct timespect *tp用来存储当前的时间,其结构如下:
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
呵呵,好啦!该讲的都刚清楚了,下面我们就上代码把;
代码
#include <iostream>
#include <time.h>
usingnamespace std;
timespec diff(timespec start, timespec end);
int main()
{
timespec time1, time2;
int temp;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
for (int i =0; i<242000000; i++)
temp+=temp;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
cout<<diff(time1,time2).tv_sec<<":"<<diff(time1,time2).tv_nsec<<endl;
return0;
}
timespec diff(timespec start, timespec end)
{
timespec temp;
if ((end.tv_nsec-start.tv_nsec)<0) {
temp.tv_sec = end.tv_sec-start.tv_sec-1;
temp.tv_nsec =1000000000+end.tv_nsec-start.tv_nsec;
} else {
temp.tv_sec = end.tv_sec-start.tv_sec;
temp.tv_nsec = end.tv_nsec-start.tv_nsec;
}
return temp;
}
函数的原型如下:
int clock_gettime(clockid_t clk_id, struct timespect *tp);
clockid_t clk_id用于指定计时时钟的类型,对于我们Programmr以下三种比较常用:
CLOCK_REALTIME, a system-wide realtime clock.
CLOCK_PROCESS_CPUTIME_ID, high-resolution timer provided by the CPU for each process.
CLOCK_THREAD_CPUTIME_ID, high-resolution timer provided by the CPU for each of the threads.
CLOCK_REALTIME, a system-wide realtime clock.
CLOCK_PROCESS_CPUTIME_ID, high-resolution timer provided by the CPU for each process.
CLOCK_THREAD_CPUTIME_ID, high-resolution timer provided by the CPU for each of the threads.
struct timespect *tp用来存储当前的时间,其结构如下:
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
呵呵,好啦!该讲的都刚清楚了,下面我们就上代码把;
代码
#include <iostream>
#include <time.h>
usingnamespace std;
timespec diff(timespec start, timespec end);
int main()
{
timespec time1, time2;
int temp;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
for (int i =0; i<242000000; i++)
temp+=temp;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
cout<<diff(time1,time2).tv_sec<<":"<<diff(time1,time2).tv_nsec<<endl;
return0;
}
timespec diff(timespec start, timespec end)
{
timespec temp;
if ((end.tv_nsec-start.tv_nsec)<0) {
temp.tv_sec = end.tv_sec-start.tv_sec-1;
temp.tv_nsec =1000000000+end.tv_nsec-start.tv_nsec;
} else {
temp.tv_sec = end.tv_sec-start.tv_sec;
temp.tv_nsec = end.tv_nsec-start.tv_nsec;
}
return temp;
}
相关文章推荐
- Linux 下使用clock_gettime给程序计时详解
- Linux下使用clock_gettime给程序计时
- Linux下使用clock_gettime给程序计时 转载
- Linux下使用clock_gettime给程序计时
- Linux下使用clock_gettime给程序计时
- clock_gettime的使用,计时比clock()精确
- 使用putty远程连接linux防止关闭putty程序就停止
- aix 使用g++编译c++程序的编译选项 -- linux到aix移植经验
- Linux下使用服务启动程序,日志乱码解决
- Linux下使用core文件调试程序
- linux下使用Makefile编译程序入门介绍
- Linux下查看某一个程序所使用的内存方法
- linux下java程序使用第三方jar包
- Linux下PF_PACKET的使用,RARP的server和client程序 (转)
- 在Linux下使用逻辑卷管理程序
- 如何在linux环境下调试JAVA程序使用访问timesten:
- 在 Linux 中限制网络带宽使用(限制指定程序和指定网卡的流量)
- Linux下使用I2C总线读写 EEPROM(读写i2c从设备通用程序)
- Xming + PuTTY 在Windows下远程Linux主机使用图形界面的程序
- linux下使用valgrind对程序进行检查