fork download
  1. #include <iostream>
  2. #include <chrono>
  3. #include <thread>
  4.  
  5. #include <conio.h>//not standard.
  6.  
  7. class StopWatch {
  8. std::chrono::high_resolution_clock::time_point S;
  9. std::chrono::high_resolution_clock::time_point E;
  10. public:
  11.  
  12. typedef std::chrono::milliseconds TimeType;
  13.  
  14. StopWatch() { Start(); };
  15.  
  16. bool Start() {
  17. S = E = std::chrono::high_resolution_clock::now();
  18. return true;
  19. }
  20. bool ReStart() {
  21. return Start();
  22. }
  23. bool Stop() {
  24. E = std::chrono::high_resolution_clock::now();
  25. return true;
  26. }
  27.  
  28. bool Reset() {
  29. S = E = std::chrono::high_resolution_clock::now();
  30. return true;
  31. }
  32. template<class T = TimeType>
  33. T Ellipse() {
  34. return std::chrono::duration_cast<T>(std::chrono::high_resolution_clock::now() - S);
  35. }
  36. template<class T = TimeType>
  37. T Result() {
  38. return std::chrono::duration_cast<T>(E - S);
  39. }
  40.  
  41. };
  42.  
  43. int main() {
  44.  
  45. int key = -1;
  46.  
  47. L1:
  48.  
  49. std::cout << "wait enter key!" << std::endl;
  50.  
  51. key = _getch();
  52. if (key == 27) {
  53. return 1;
  54. }
  55. std::this_thread::sleep_for(std::chrono::nanoseconds(1));
  56. if (key != '\r') goto L1;
  57.  
  58. StopWatch SW;
  59. key = -1;
  60.  
  61. std::cin.clear();
  62.  
  63. SW.Start();
  64. while (key != '\r') {
  65. key = -1;
  66. std::cout << "wait enter key!" << std::endl;
  67. std::this_thread::sleep_for(std::chrono::nanoseconds(1));
  68. if (_kbhit()) {
  69. key = _getch();
  70. if (key != '\r') {
  71. std::cout << key << "is not enter key!" << std::endl;
  72. }
  73. }
  74. if (key == 27) {
  75. return 1;
  76. }
  77. }
  78. SW.Stop();
  79.  
  80. std::cout << "Count:" << SW.Result().count()<<"ms." << std::endl;
  81.  
  82. std::cin.clear();
  83.  
  84. return 0;
  85. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:5:10: fatal error: conio.h: No such file or directory
 #include <conio.h>//not standard.
          ^~~~~~~~~
compilation terminated.
stdout
Standard output is empty