Created
June 12, 2019 16:41
-
-
Save ppLorins/cfbbbab9c924009602bafcabcba30352 to your computer and use it in GitHub Desktop.
std::chrono::system_clock::now() precision problem under windows .
This file contains hidden or 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 <random> | |
#include <chrono> | |
int main(int argc, char** argv){ | |
char sz_time_buf[1024] = { 0 }; | |
std::random_device rd; | |
std::mt19937 gen(rd()); | |
std::uniform_int_distribution<unsigned long> dis(1,1000); | |
for (int i = 0; i < 20; ++i) { | |
auto _now = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()); | |
std::snprintf(sz_time_buf,sizeof(sz_time_buf),".%llu",_now.count()); | |
std::cout << "time:" << sz_time_buf << std::endl; | |
std::this_thread::sleep_for(std::chrono::microseconds(dis(gen))); | |
} | |
return 0; | |
} | |
/* | |
1. mac output: | |
time:.1560357052 644 707 | |
time:.1560357052 645 032 | |
time:.1560357052 646 144 | |
time:.1560357052 646 998 | |
time:.1560357052 648 005 | |
time:.1560357052 648 319 | |
time:.1560357052 648 765 | |
time:.1560357052 648 977 | |
time:.1560357052 649 798 | |
time:.1560357052 651 150 | |
time:.1560357052 652 017 | |
time:.1560357052 652 113 | |
time:.1560357052 652 459 | |
time:.1560357052 653 264 | |
time:.1560357052 653 786 | |
time:.1560357052 654 064 | |
time:.1560357052 655 173 | |
time:.1560357052 656 326 | |
time:.1560357052 656 718 | |
time:.1560357052 657 277 | |
Note: microseconds part discrimination of the timestamp is normal. | |
2. windows output: | |
time:.1560357132 557 687 | |
time:.1560357132 560 688 | |
time:.1560357132 563 689 | |
time:.1560357132 565 692 | |
time:.1560357132 567 689 | |
time:.1560357132 570 689 | |
time:.1560357132 572 688 | |
time:.1560357132 574 689 | |
time:.1560357132 576 692 | |
time:.1560357132 579 687 | |
time:.1560357132 582 693 | |
time:.1560357132 585 688 | |
time:.1560357132 588 687 | |
time:.1560357132 590 687 | |
time:.1560357132 592 688 | |
time:.1560357132 595 690 | |
time:.1560357132 597 688 | |
time:.1560357132 599 689 | |
time:.1560357132 601 689 | |
time:.1560357132 603 690 | |
Note: microseconds part discrimination of the timestamp is quite bad. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment