[C/C++11]_[初级]_[实用时间库chrono]
来源:互联网 发布:战地4优化怎么样 编辑:程序博客网 时间:2024/06/08 08:08
场景
- C++11开始提供了一个 实用时间库,相比C的time.h强多了,最明显的是可以精确到纳秒.
- vs2010还不支持这个库,所以比较可惜,对于新开发的项目,建议用2015可以使用最新特性.
- 一般用这个库来转换时间或计算函数执行时间或者CPU指令周期.
例子
#include <iostream> #include <algorithm>#include <sstream>#include <stdlib.h> #include <string.h> #include <string>#include <assert.h>#include <ctype.h> #include <vector> #include <Windows.h>#include <chrono>#include <iomanip>#include <stdint.h>using namespace std; class A{public: int i_;};int64_t Factorial(int gene){ return (gene == 1)?1:gene*Factorial(gene-1);}void TestChrono(){ // 计算函数执行时间. auto start = std::chrono::steady_clock::now(); std::vector<A*> v; for (int i = 1; i <= 1000000; ++i) { v.push_back(new A()); } for (auto a : v) { delete a; } auto end = std::chrono::steady_clock::now(); std::chrono::duration<double> diff = end-start; std::cout << "=>Time Elapse " << diff.count() << " s" << std::endl; // 获取过去24小时的时间 std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); std::time_t now_c = std::chrono::system_clock::to_time_t(now - std::chrono::hours(24)); char buf[32]; strftime(buf,32,"%Y-%m-%d %H:%M:%S",std::localtime(&now_c)); std::cout << buf << std::endl; // time_t t; // t = time(NULL); // std::stringstream ss; // ss << put_time(localtime(&t),"%Y-%m-%d %H:%M:%S"); // std::string str = ss.str(); // std::cout << "str: " << str << std::endl;}int main(int argc, char const *argv[]) { TestChrono(); return 0; }
输出
=>Time Elapse 0.35702 s2016-05-29 14:26:52
参考
chrono
time_point
put_time
0 0
- [C/C++11]_[初级]_[实用时间库chrono]
- [C/C++标准库]_[初级]_[实用类std::pair]
- [C/C++标准库]_[初级]_[转换UTC时间到local本地时间]
- [C/C++]_[初级]_[原子操作]
- [C/C++标准库]_[初级]_[使用时间库]
- [C/C++11语法]_[初级]_[lamba 表达式介绍]
- [C/C++11]_[初级]_[使用enumerations类型]
- [C/C++标准库]_[初级]_[signal信号浅析]
- [C/C++标准库]_[初级]_[集合操作]
- [C/C++标准库]_[初级]_[使用string stream]
- [C/C++标准库]_[初级]_[分割字符串Split]
- [C/C++标准库]_[初级]_[unary_function 和 binary_function]
- [C/C++标准库]_[初级]_[unary_function 和 binary_function]
- [Object C]_[初级]_[NSArray排序]
- [Object C]_[初级]_[文件修改创建时间和修改时间]
- [C/C++]_[初级]_[C语言编译过程]
- [C/C++11]_[初级]_[使用正则表达式库regex]
- [C/C++11]_[初级]_[使用正则表达式库进行分组查询]
- 栈、队列和链表
- Android 读取OTG中配置文件完成自动测试
- WebView内存泄漏记录
- 242. Valid Anagram
- NSURLSession 同步请求(使用信号量)
- [C/C++11]_[初级]_[实用时间库chrono]
- c++实现的虚拟光标
- XCode7之后如何做真机测试
- 2016春季练习——DP水题
- 2016.5.30---servlet心得
- android开发游记:多点触控解析与运用
- service中加入dialog
- Cocos2d 内存优化
- python之函数总结1