首页 > 编程语言 >c/c++ 时间函数

c/c++ 时间函数

时间:2022-10-02 17:12:44浏览次数:46  
标签:end 函数 c++ start tag 时间 func printf test

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;
}

标签:end,函数,c++,start,tag,时间,func,printf,test
From: https://www.cnblogs.com/shiguoliang/p/16533089.html

相关文章

  • 从IDA实战看函数参数和局部变量在栈里分布的差异
    问题:IDAPro识别了在0x10001656处的子过程(函数)中的多少个局部变量? 当前版本的IDA,识别的是一个参数lpThreadParameter,以及62个参数局部参数通常以var_开头,偏移值为负值,......
  • Mysql function 自定义函数,查找子节点
    ThisfunctionhasnoneofDETERMINISTIC,NOSQL,orREADSSQLDATAinitsdeclarationandbinaryloggingisenabled(you*might*wanttousethelesssafelog_......
  • 函数学习
    1.函数分为:库函数和自定义函数查找库函数的网站:http://www.cplusplus.com   或着​​https://legacy.cplusplus.com/reference/​​中文版的C语言函数网站:cpprefenrer......
  • 《Effective C++:改善程序与设计的55个具体做法》阅读笔记
    Item13:使用对象管理资源资源管理对象:资源管理对象管理着其他对象的资源,当资源管理对象的析构函数被调用时,所管理的资源会被自动释放。资源管理对象就是在其析构函数中......
  • React18 (六) hook钩子函数
    React中的钩子函数的功能非常的强大,而它的使用又十分简单。关于钩子函数的使用,我们只需记住两点:1.钩子只能在React组件和自定义钩子中使用2.钩子不能在嵌套函数或其他......
  • C++——史蒂夫
    #include<iostream>#include<string>#include<windows.h>#include"minecraft.h"usingnamespacestd;TxMinecraftmc;intmain(intargc,char**argv){//连接我的世......
  • gym.ObservationWrapper使用时的注意点——reset和step函数可以覆盖observation函数
    记录一个刚学习到的gym使用的点,就是gym.ObservationWrapper使用时的注意点——reset和step函数可以覆盖observation函数。  给出代码:importgymclassWrapper(gym.Observa......
  • C/C++ 实现INI配置文件读写 [转载]
    INI文件是一种标准的Windows平台配置文件,通常这种配置文件用于保存系统软件的一些基本配置参数,如下代码是本人从网络上收集到的一段纯C++编写的配置解析......
  • 函数进阶
    一、多函数程序执行流程(一)共用全局变量#定义全局变量num=0deftest1():globalnum#修改全局变量num=100deftest2():#调用test1函数中修......
  • C++实现双向RRT算法
    C++实现双向RRT算法背景介绍RRT(Rapidly-exploringRandomTrees)是StevenM.LaValle和JamesJ.KuffnerJr.提出的一种通过所及构建空间搜索树实现对非凸高维空间快速搜......