读取系统时间

头文件:

#include <sys/time.h>

示例:
TEST(maxDistance, t0)
{
    Solution st;
    struct timeval tv1, tv2;
    gettimeofday(&tv1, NULL);
    printf("second: %ld\n", tv1.tv_sec); //
    printf("millisecond: %ld\n", tv1.tv_sec * 1000 + tv1.tv_usec / 1000); // 毫秒
    printf("microsecond: %ld\n", tv1.tv_sec * 1000000 + tv1.tv_usec); // 微秒

    int max = st.maxDistance(g_leetcodemaxDistancetest1162);
    cout << "max:" << max << endl;
    sleep(3);
    gettimeofday(&tv2, NULL);
    printf("second: %ld\n", tv2.tv_sec); //
    printf("millisecond: %ld\n", tv2.tv_sec * 1000 + tv2.tv_usec / 1000); // 毫秒
    printf("microsecond: %ld\n", tv2.tv_sec * 1000000 + tv2.tv_usec); // 微秒

    cout << "cost time :" << tv2.tv_sec - tv1.tv_sec << "" << endl;
    cout << "cost time :" << (tv2.tv_sec * 1000 + tv2.tv_usec / 1000) - (tv1.tv_sec * 1000 + tv1.tv_usec / 1000) << "毫秒" << endl;
    cout << "cost time :" << (tv2.tv_sec * 1000000 + tv2.tv_usec) - (tv1.tv_sec * 1000000 + tv1.tv_usec) << "微秒" << endl;

}

输出:

 

posted on 2022-04-08 21:09  蜀山菜鸟  阅读(38)  评论(0)    收藏  举报