微秒
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
int main(void){
struct timeval timeStart, timeEnd, timeSystemStart;
double runTime=0, systemRunTime;
gettimeofday(&timeSystemStart, NULL );
while(1){
gettimeofday(&timeStart, NULL );
usleep(300000);
gettimeofday( &timeEnd, NULL );
runTime = (timeEnd.tv_sec - timeStart.tv_sec ) + (double)(timeEnd.tv_usec -timeStart.tv_usec)/1000000;
systemRunTime = (timeEnd.tv_sec - timeSystemStart.tv_sec ) + (double)(timeEnd.tv_usec -timeSystemStart.tv_usec)/1000000;
printf("runTime is %lf\n", runTime);
printf("systemRunTime is %lf\n", systemRunTime);
}
return 0;
}
纳秒
clock_gettime(CLOCK_REALTIME, &tim);
std::cout<<tim.tv_nsec <<std::endl;
c++获得当前是间(精确到秒)
#include<iostream>
#include<time.h>
using namespace std;
void main()
{
//获取系统时间
time_t now_time=time(NULL);
//获取本地时间
tm* t_tm = localtime(&now_time);
//转换为年月日星期时分秒结果,如图:
printf("local time is : %s\n", asctime(t_tm));
//将时间转换为秒
time_t mk_time = mktime(t_tm);
//也可以自己定义一个时间
//定义截止时间
struct tm deadline_tm;
deadline_tm.tm_sec=0;//[0~59]
deadline_tm.tm_min=10;//[0~59]
deadline_tm.tm_hour=13;//[0~23]
deadline_tm.tm_isdst=0;//default
deadline_tm.tm_mday=31;//[1~31]
deadline_tm.tm_mon=2;//[0~11]
}
网友评论