C++ 获取时间


C++ 两种获取时间的方式

  1. 使用 #include <sys/time.h> 下面的 gettimeofday函数
// 获取微秒
long GetCurrentMicroseconds(){
    timeval time;
    gettimeofday(&time,nullptr);
    return (time.tv_sec * 1000000 + time.tv_usec);
}

如果需要毫秒,则 return (time.tv_sec * 1000 + time.tv_usec/1000)

  1. 使用 #include 下面的 chrono库
    // 获取微秒
int64_t CurrentTimeMillis(){
    auto now = chrono::system_clock::now();
    return chrono::duration_cast<chrono::microseconds>(now.time_since_epoch()).count();
}

如果需要的是毫秒,则 return chrono::duration_castchrono::milliseconds(now.time_since_epoch()).count();


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM