zoukankan      html  css  js  c++  java
  • linux中的gettimeofday方法

    测试调用delya()函数所需执行的时间(单位为微妙)
    #include<stdio.h>
    #include<sys/time.h>
    #include<unistd.h>
    int delay(int time)
    {
        int i,j;
        for(i =0;i<time;i++)
            for(j=0;j<5000;j++)
                ;
    }
    
    int main()
    {
            struct  timeval start;
            struct  timeval end;
            unsigned  long diff;
            gettimeofday(&start,NULL);
            delay(10);
            gettimeofday(&end,NULL);
            diff = 1000000 * (end.tv_sec-start.tv_sec)+ end.tv_usec-start.tv_usec;
            printf(“thedifference is %ld
    ”,diff);
            return 0;
    }

    linux中获取字符串时间

    string get_local_time()
    {
        char time_str[60]={0};
        struct timeval tvpre;
        gettimeofday(&tvpre, NULL);
        struct tm* ptm;
        char time_string[40];
        long milliseconds;
        ptm = localtime (&(tvpre.tv_sec));
        /* 格式化日期和时间,精确到秒为单位。*/
        //strftime (time_string, sizeof(time_string), "%Y/%m/%d %H:%M:%S", ptm); //输出格式为: 2018/12/09 10:48:31.391
        //strftime (time_string, sizeof(time_string), "%Y|%m|%d %H-%M-%S", ptm); //输出格式为: 2018|12|09 10-52-28.302
        strftime (time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", ptm); //输出格式为: 2018-12-09 10:52:57.200
        //strftime (time_string, sizeof(time_string), "%Y\%m\%d %H-%M-%S", ptm); //输出格式为: 2018129 10-52-28.302
        /* 从微秒计算毫秒。*/
        milliseconds = tvpre.tv_usec / 1000;
        /* 以秒为单位打印格式化后的时间日期,小数点后为毫秒。*/
        snprintf (time_str, sizeof(time_str), "%s.%03ld", time_string, milliseconds);
        return string(time_str);
    }
    string GetSystemTime()
    {
        time_t timep;time (&timep);
        char tmp[64];
        strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S",localtime(&timep));
        string s(tmp);
        return s;
    }
  • 相关阅读:
    【python-opencv】opencv基础操作之一
    【胎教】做AI的基础,开始学习。
    【实习】博士生找实习的囧事之其一
    【经验】CS
    【keras】用tensorboard监视CNN每一层的输出
    【算法】背包九讲
    【计算机网络】大数据 云计算 人工智能
    【算法】shortest distance
    【git】git hello world
    【算法】深度优先 马走日 Hamilton routes
  • 原文地址:https://www.cnblogs.com/mathyk/p/12792593.html
Copyright © 2011-2022 走看看