本文主要是介绍C++之时间戳耗时对比(九十一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.代码示例
//1.demo_01.cpp
#include<iostream>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>int main(){struct timeval tv;unsigned long sec_start,sec_end,ms_start,ms_end,us_start,us_end;//start timegettimeofday(&tv,NULL);sec_start = tv.tv_sec; //secms_start = tv.tv_sec*1000 + tv.tv_usec/1000; //msus_start = tv.tv_sec*1000000 + tv.tv_usec; //usprintf("sleep 2s\n");sleep(2); //end timegettimeofday(&tv,NULL);sec_end = tv.tv_sec; //secms_end = tv.tv_sec*1000 + tv.tv_usec/1000; //msus_end = tv.tv_sec*1000000 + tv.tv_usec; //usprintf("sec_delta = %ld s\n",sec_end - sec_start);printf("ms_delta = %ld ms\n",ms_end - ms_start);printf("us_delta = %ld us\n",us_end - us_start);
}
//2.demo_02.cpp
#include<iostream>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>struct timeval tv;
unsigned long us_start,us_end;
void start_time(){gettimeofday(&tv,NULL);us_start = tv.tv_sec*1000000 + tv.tv_usec; //us
}void end_time(char *func, int line){gettimeofday(&tv,NULL);us_end = tv.tv_sec*1000000 + tv.tv_usec; //usprintf("%s, line = %d, us_delta = %ld us \n",func,line,us_end - us_start);
}int main(){start_time();sleep(2);end_time("start_preview()", __LINE__);
}
这篇关于C++之时间戳耗时对比(九十一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!