fork download
  1. /** @file
  2.  * @author Chuan-Bin "Bill" Huang
  3.  * @date 2013-06-27
  4.  * @version 1.0
  5.  *
  6.  * @brief A Brief Demo of C++11 <chrono> and <random>
  7.  * @section References
  8.  *
  9.  * Original BBS posts:
  10.  *
  11.  * <a href="https://w...content-available-to-author-only...t.cc/bbs/C_and_CPP/M.1372284252.A.99D.html">C++11 random</a>
  12.  *
  13.  * <a href="https://w...content-available-to-author-only...t.cc/bbs/C_and_CPP/M.1372283467.A.A74.html">C++11 chrono</a>
  14.  *
  15.  */
  16.  
  17. #include <iostream>
  18. #include <random>
  19. #include <chrono>
  20. using namespace std::chrono;
  21. // (wrong) using namespace std::random;
  22.  
  23. int main()
  24. {
  25. // Experimental Parameters
  26. size_t start_time = 0, end_time = 0, for_time = 0; // speed record
  27. unsigned int N = 1000000;
  28. double *a = new double [N]; // array of random numbers
  29. unsigned int i=0;
  30. std::normal_distribution<double> dist(0,1); // N(0,1) distribution
  31.  
  32. // Format Conversion
  33. printf("1.Test duration_cast<precision>(duration_object).count() method:\n");
  34. size_t t64 = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  35. printf(" time now in dec ns: %u (uint64, directly obtained)\n", t64);
  36. printf(" in hex : %16llx\n\n", t64);
  37.  
  38. printf("2.make sure uint64 -> uint32 discards higher bits(suitable for seeding):\n");
  39. unsigned long t32 = static_cast<unsigned long>(t64);
  40. printf(" time now in hex ns: %16x (truncated to uint32)\n\n", t32);
  41.  
  42. printf("3.generating %u N(0,1) random numbers using different engines...\n", N);
  43.  
  44. // Time cost of for-loop body
  45. start_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  46. for(i=0; i<N; i++){}
  47. end_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  48. for_time = end_time - start_time;
  49. printf("%16u ns used on for-loop body\n", for_time);
  50.  
  51. // mt19937 engine
  52. start_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  53. std::mt19937 mt(static_cast<unsigned long>(duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count()));
  54. for(i=0; i<N; i++)
  55. a[i] = dist(mt);
  56. end_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  57. printf("%16u ns for mt19937\n", (end_time-start_time-for_time));
  58.  
  59. // mt19937_64 engine
  60. start_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  61. std::mt19937_64 mt64(static_cast<unsigned long>(duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count()));
  62. for(i=0; i<N; i++)
  63. a[i] = dist(mt64);
  64. end_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  65. printf("%16u ns for mt19937_64\n", (end_time-start_time-for_time));
  66.  
  67. // minstd_rand engine
  68. start_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  69. std::minstd_rand min_rnd(static_cast<unsigned long>(duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count()));
  70. for(i=0; i<N; i++)
  71. a[i] = dist(min_rnd);
  72. end_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  73. printf("%16u ns for minstd_rand\n", (end_time-start_time-for_time));
  74.  
  75. // default_random_engine
  76. start_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  77. std::default_random_engine def_rnd(static_cast<unsigned long>(duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count()));
  78. for(i=0; i<N; i++)
  79. a[i] = dist(def_rnd);
  80. end_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  81. printf("%16u ns for default_random_engine\n", (end_time-start_time-for_time));
  82.  
  83. // random_device (hardware generator)
  84. start_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  85. std::random_device rd;
  86. for(i=0; i<N; i++)
  87. a[i] = dist(rd);
  88. end_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  89. printf("%16u ns for random_device\n", (end_time-start_time-for_time));
  90.  
  91. delete [] a;
  92. printf("\nProgram Ended! Press Enter to quit.");
  93. std::cin.get();
  94. return 0;
  95. }
  96.  
Success #stdin #stdout 0.52s 16072KB
stdin
Standard input is empty
stdout
1.Test duration_cast<precision>(duration_object).count() method:
  time now in dec ns: 1251234941 (uint64, directly obtained)
           in hex   : 14e9e8b14a94547d

2.make sure uint64 -> uint32 discards higher bits(suitable for seeding):
  time now in hex ns:         4a94547d (truncated to uint32)

3.generating 1000000 N(0,1) random numbers using different engines...
             151 ns used on for-loop body
        42934807 ns for mt19937
        33733544 ns for mt19937_64
        33397393 ns for minstd_rand
        32978185 ns for default_random_engine
       380725676 ns for random_device

Program Ended! Press Enter to quit.