Created
June 18, 2014 15:55
-
-
Save mitsu-ksgr/6615d555e6437b1de385 to your computer and use it in GitHub Desktop.
C++11 で時間を取得するテストコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <chrono> | |
// for sleep | |
#include <thread> | |
void sleep(unsigned long sleep_time) | |
{ | |
std::chrono::milliseconds duration(sleep_time); | |
std::this_thread::sleep_for(duration); | |
} | |
/*! | |
* @brief エポック時間をミリ秒単位で取得する。 | |
*/ | |
unsigned long getMilliSec() | |
{ | |
return static_cast<unsigned int>( | |
std::chrono::system_clock::now().time_since_epoch() / | |
std::chrono::milliseconds(1) ); | |
} | |
int main() | |
{ | |
//------------------------------ | |
// system_clock test | |
std::cout << "* chrono::sytem_clock::Now()" << std::endl; | |
auto const st1 = std::chrono::system_clock::now(); | |
std::cout << "Start Time: " << st1.time_since_epoch().count() << std::endl; | |
sleep(1500); | |
auto const et1 = std::chrono::system_clock::now(); | |
std::cout << "End Time: " << et1.time_since_epoch().count() << std::endl; | |
auto elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(et1 - st1); | |
std::cout << "Elapsed Time: " << elapsed_time.count() << std::endl; | |
//------------------------------ | |
// getMilliSec test | |
std::cout << "* getMilliSec" << std::endl; | |
auto st2 = getMilliSec(); | |
std::cout << "Start Time: " << st2 << "[ms]" << std::endl; | |
sleep(3000); | |
auto et2 = getMilliSec(); | |
std::cout << "End Time: " << et2 << "[ms]" << std::endl; | |
std::cout << "Elapsed TIme: " << (et2 - st2) << "[ms]" << std::endl; | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment