c/c++ 时间函数

c语言相关函数

#include <stdio.h>
#include <time.h>

void time_test_func()
{
    time_t seconds;
    seconds = time(NULL);

    printf("从1970-01-01 00:00:00到现在的秒数: [%ld]\n", seconds);
}

void difftime_test_func()
{
    time_t start, end;
    double delta;

    start = time(NULL);
    printf("Hello world!\n");
    end = time(NULL);
    delta = difftime(end, start);

    printf("相差秒数 : %0.2f\n", delta);
}

void strftime_test_func()
{
    time_t seconds;
    struct tm* now;
    char buf[1024];

    seconds = time(NULL);
    now = localtime(&seconds);
    strftime(buf, 1024, "%Y-%m-%d %H:%M:%S", now);

    printf("格式化的日期 & 时间 : [%s]\n", buf);
}

int main()
{
    time_test_func();
    difftime_test_func();
    strftime_test_func();
    return 0;
}

linux相关函数

#include <stdio.h>
#include <sys/time.h>

void gettimeofday_test_func()
{
    struct timeval start, end;
    double delta;

    gettimeofday(&start, NULL);
    printf("Hello world!\n");
    gettimeofday(&end, NULL);
    delta = (end.tv_sec + end.tv_usec / 1000000.0) - (start.tv_sec + start.tv_usec / 1000000.0);

    printf("start sec: %ld usec %ld\n", start.tv_sec, start.tv_usec);
    printf("end sec: %ld usec %ld\n", end.tv_sec, end.tv_usec);
    printf("run time: %f s\n", delta);
}

int main()
{
    gettimeofday_test_func();
    return 0;
}

计算算法时间


#include <stdio.h>
#include <time.h>

void clock_test_func()
{
    clock_t start, end;
    double delta;

    start = clock();
    printf("Hello world!\n");
    end = clock();
    delta = (double)(end - start) / CLOCKS_PER_SEC;

    printf("CPU 占用时间:%f s\n", delta);
}

int main()
{
    clock_test_func();
    return 0;
}

c++时间函数

#include <iostream>
#include <chrono>
#include <ctime>
#include <unistd.h>

using namespace std;

void system_clock_test_func()
{
    auto now = chrono::system_clock::now();
    time_t time = chrono::system_clock::to_time_t(now);
    cout << "now: " << ctime(&time) << endl;
}

void steady_clock_test_func()
{
    auto start = chrono::steady_clock::now();
    sleep(1);
    auto end = chrono::steady_clock::now();

    auto diff = end - start;
    auto sec = chrono::duration_cast<chrono::seconds>(diff);
    auto ms = chrono::duration_cast<chrono::milliseconds>(diff);

    cout << "duration: " << sec.count() << " s" << endl;
    cout << "duration: " << ms.count() << " ms" << endl;
    cout << "duration: " << diff.count() << " us" << endl;
}

#define TIMERSTART(tag)  auto tag##_start = std::chrono::steady_clock::now(),tag##_end = tag##_start
#define TIMEREND(tag)  tag##_end =  std::chrono::steady_clock::now()
#define DURATION_s(tag) printf("%s costs %d s\n",#tag,std::chrono::duration_cast<std::chrono::seconds>(tag##_end - tag##_start).count())
#define DURATION_ms(tag) printf("%s costs %d ms\n",#tag,std::chrono::duration_cast<std::chrono::milliseconds>(tag##_end - tag##_start).count());
#define DURATION_us(tag) printf("%s costs %d us\n",#tag,std::chrono::duration_cast<std::chrono::microseconds>(tag##_end - tag##_start).count());
#define DURATION_ns(tag) printf("%s costs %d ns\n",#tag,std::chrono::duration_cast<std::chrono::nanoseconds>(tag##_end - tag##_start).count());

int macro_test()
{
    TIMERSTART(for_loop);
    sleep(1);
    TIMEREND(for_loop);

    DURATION_ms(for_loop);
}

int main()
{
    system_clock_test_func();
    steady_clock_test_func();
    macro_test();
    return 0;
}
posted @ 2022-10-02 16:58  暹罗吹雪  阅读(59)  评论(0编辑  收藏  举报