Skip to content

Instantly share code, notes, and snippets.

@HongfeiXu
Last active January 20, 2018 06:25
Show Gist options
  • Save HongfeiXu/9574873fa40f683ca40316f4b223f44d to your computer and use it in GitHub Desktop.
Save HongfeiXu/9574873fa40f683ca40316f4b223f44d to your computer and use it in GitHub Desktop.
使用 C++11 中的 chrono 库 测量时间
/*
时间测量
使用 C++11 中的 chrono 库
http://en.cppreference.com/w/cpp/chrono/duration/duration_cast
*/
#include <chrono>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v;
auto t0 = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 10000; ++i)
v.push_back(i);
auto t1 = std::chrono::high_resolution_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0).count() << "ms\n";
return 0;
}
/*
6ms
请按任意键继续. . .
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment