linux__时间__操作

来源:互联网 发布:拳击手套 知乎 编辑:程序博客网 时间:2024/06/03 23:53
sno_guo的博客,用来记录平时用到的一些小函数.不断更新中........
unsigned int get_current_time(){     time_t timep;     struct tm *p;     int year;     time(&timep);     p=localtime(&timep);      year=(int)(p->tm_year-100);     if(year<0)        year=0;      return   ((year<<26)                 | ((unsigned int)((1+p->tm_mon)&0xf)<<22)                | ((unsigned int)((p->tm_mday)&0x1f)<<17)                | ((unsigned int)((p->tm_hour)&0x1f)<<12)                | ((unsigned int)((p->tm_min)&0x3f)<<6)                | ((p->tm_sec)&0x3f));/* unsigned int returntime = get_current_time();  ////这里仅仅是测试代码         printf("return time is 0x%x\n",returntime);                             int second,minute,hour,day,month,year;       second = returntime & 0x3f;     minute = (returntime >> 6) & 0x3f;     hour = (returntime >> 12) & 0x1f;     day = (returntime >> 17) & 0x1f;     month = (returntime >> 22) & 0xf;     year = 2000 + (returntime >> 26);   printf ("%d,%d,%d,%d:%d:%d\n", year,month,day,hour,minute,second); */}


计算一段时间使用了多少时间的方法:

#include <sys/time.h>
struct timeval tpstart,tpend; float timeuse;gettimeofday(&tpstart,NULL);         //这里加上耗时的函数执行部分gettimeofday(&tpend,NULL);  timeuse=(tpend.tv_sec*1000+tpend.tv_usec/1000)-(tpstart.tv_sec*1000+tpstart.tv_usec/1000);  __android_log_print(ANDROID_LOG_INFO, "BroovPlayer", "updateSurface time is %f",timeuse);  //这里是打印,时间单位是ms 

下面是读取当前日期时间:

#include <sys/time.h>
time_t tCurrentTime;
struct tm *tmnow;
struct timeval tv;
time(&tCurrentTime);
gettimeofday(&tv,NULL);
tmnow = localtime(&tCurrentTime);
sprintf(strFileName, "%04d%02d%02d%02d%02d%02d%03ld.jpg",
tmnow->tm_year+1900, tmnow->tm_mon+1, tmnow->tm_mday,
tmnow->tm_hour, tmnow->tm_min, tmnow->tm_sec, tv.tv_usec / 1000);
return 0;


得到当前系统时间的毫秒数:  struct timeval tv = {0};    gettimeofday(&tv, 0);    return tv.tv_usec/1000;

#ifdef AV_READ_TIME#define START_TIMER                             \    uint64_t tend;                              \    uint64_t tstart = AV_READ_TIME();           \#define STOP_TIMER(id)                                                    \    tend = AV_READ_TIME();                                                \    {                                                                     \        static uint64_t tsum   = 0;                                       \        static int tcount      = 0;                                       \        static int tskip_count = 0;                                       \        if (tcount < 2                        ||                          \            tend - tstart < 8 * tsum / tcount ||                          \            tend - tstart < 2000) {                                       \            tsum+= tend - tstart;                                         \            tcount++;                                                     \        } else                                                            \            tskip_count++;                                                \        if (((tcount + tskip_count) & (tcount + tskip_count - 1)) == 0) { \            av_log(NULL, AV_LOG_ERROR,                                    \                   "%"PRIu64" decicycles in %s, %d runs, %d skips\n",     \                   tsum * 10 / tcount, id, tcount, tskip_count);          \        }                                                                 \    }#else#define START_TIMER#define STOP_TIMER(id) { }#endif

#include <stdio.h>#include <inttypes.h>///#define DEBUG_X86typedef unsigned long long uint64_t;typedef unsigned int uint32_t;/*将计算机启动以来的CPU运行周期数放到EDX:EAX里面,EDX是高位,EAX是低位。  CPU运行周期数指的是CPU的一个时钟触发吧,就是应该是一个上升沿或者一个下降沿表示一个周期。  有一点你应该明白:周期数/CPU主频 = CPU通电以来的执行秒数,也就是GetTickcount干的事情。  或者这样说使用两次RDTSC,把两次的结果相减,得到“间隔周期数”,间隔周期数/CPU主频=CPU执行这两条指令间的秒数  这样我们就能使用太来做一些anti了*//*static inline uint64_t read_time(void){    uint32_t a, d;    __asm__ volatile("rdtsc" : "=a" (a), "=d" (d));    return ((uint64_t)d << 32) + a;}*/#if defined(__ARM_ARCH_7A__) static inline uint64_t read_time(void){    unsigned cc;    __asm__ volatile ("mrc p15, 0, %0, c9, c13, 0" : "=r"(cc)); ///这个是 ARMV7指令集    return cc;}#endif#define AV_READ_TIME read_time#ifdef AV_READ_TIME#define START_TIMER                             \    uint64_t tend;                              \    uint64_t tstart = AV_READ_TIME();           \#define STOP_TIMER(id)                                                    \    tend = AV_READ_TIME();                                                \    {                                                                     \        static uint64_t tsum   = 0;                                       \        static int tcount      = 0;                                       \        static int tskip_count = 0;                                       \        if (tcount < 2                        ||                          \            tend - tstart < 8 * tsum / tcount ||                          \            tend - tstart < 2000) {                                       \            tsum+= tend - tstart;                                         \            tcount++;                                                     \        } else                                                            \            tskip_count++;                                                \        if (((tcount + tskip_count) & (tcount + tskip_count - 1)) == 0) { \             printf( "%"PRIu64"decicycles in %s, %d runs, %d skips\n",  \                   tsum*10/tcount, id, tcount, tskip_count);          \        }                                                                 \    }#else#define START_TIMER#define STOP_TIMER(id) { }#endif #define LOOP_TIME 1024*1024int main(){   int buf[LOOP_TIME];      printf("sizeof(unsigned long long) is:%d\n",sizeof(unsigned long long));      START_TIMER   for(int i=0;i<LOOP_TIME;i++)   {     buf[i]=i;   }STOP_TIMER("---->>---")return 0;}/*====================================另一个的测试:在32位平台 typedef unsigned long long int  uint64_t;在64位平台 typedef unsigned long int   uint64_t;不同的typdef,要求在printf中使用不同的length modifier,uint64_t 在32位使用ll,在64位使用l。除了定义数据类型,C99还定义了相应数据类型的打印方式,使用PRIu64打印uint64,举例如下:  1 #include <stdio.h> 2 #include <inttypes.h> 3   4 int main(int argc, char *argv[]) 5 { 6     uint64_t u64 = 100; 7     printf("uint64: %"PRIu64"\n", u64); 8     // printf("uint64: %lu\n", u64);   x86_84 9     // printf("uint64: %llu\n", u64);  x8610     return 0;11 }*/


原创粉丝点击