fork(1) download
  1. #include <iostream>
  2. #include <random>
  3. #include <chrono>
  4. using namespace std::chrono;
  5.  
  6. int main()
  7. {
  8. // variables
  9. size_t start_time = 0, end_time = 0, for_time = 0; // speed record
  10. unsigned int N = 100000;
  11. double *a = new double [N]; // array of random numbers
  12. unsigned int i=0;
  13. std::normal_distribution<double> dist(0,1); // N(0,1) distribution
  14.  
  15. printf("1.Test duration_cast<precision>(duration_object).count() method:\n");
  16. size_t t64 = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  17. printf(" time now in dec ns: %u (uint64, directly obtained)\n", t64);
  18. printf(" in hex : %16llx\n\n", t64);
  19.  
  20. printf("2.make sure uint64 -> uint32 discards higher bits(suitable for seeding):\n");
  21. unsigned long t32 = static_cast<unsigned long>(t64);
  22. printf(" time now in hex ns: %16x (truncated to uint32)\n\n", t32);
  23.  
  24. printf("3.generating %u N(0,1) random numbers using different engines...\n", N);
  25.  
  26. // Time cost of for-loop body
  27. start_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  28. for(i=0; i<N; i++){}
  29. end_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  30. for_time = end_time - start_time;
  31. printf("%16u ns used on for-loop body\n", for_time);
  32.  
  33. // mt19937 engine
  34. start_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  35. std::mt19937 mt(static_cast<unsigned long>(duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count()));
  36. for(i=0; i<N; i++)
  37. a[i] = dist(mt);
  38. end_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  39. printf("%16u ns for mt19937\n", (end_time-start_time-for_time)) ;
  40.  
  41. // mt19937_64 engine
  42. start_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  43. std::mt19937_64 mt64(static_cast<unsigned long>(duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count()));
  44. for(i=0; i<N; i++)
  45. a[i] = dist(mt64);
  46. end_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  47. printf("%16u ns for mt19937_64\n", (end_time-start_time-for_time)) ;
  48.  
  49. // minstd_rand engine
  50. start_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  51. std::minstd_rand min_rnd(static_cast<unsigned long>(duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count()));
  52. for(i=0; i<N; i++)
  53. a[i] = dist(min_rnd);
  54. end_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  55. printf("%16u ns for minstd_rand\n", (end_time-start_time-for_time)) ;
  56.  
  57. // default_random_engine
  58. start_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  59. std::default_random_engine def_rnd(static_cast<unsigned long>(duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count()));
  60. for(i=0; i<N; i++)
  61. a[i] = dist(def_rnd);
  62. end_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  63. printf("%16u ns for default_random_engine\n", (end_time-start_time-for_time)) ;
  64.  
  65. // random_device (hardware generator)
  66. start_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  67. std::random_device rd;
  68. for(i=0; i<N; i++)
  69. a[i] = dist(rd);
  70. end_time = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
  71. printf("%16u ns for random_device\n", (end_time-start_time-for_time)) ;
  72.  
  73. delete [] a;
  74. system("PAUSE");
  75. return 0;
  76. }
Success #stdin #stdout #stderr 0.36s 3036KB
stdin
Standard input is empty
stdout
1.Test duration_cast<precision>(duration_object).count() method:
  time now in dec ns: 1292916176 (uint64, directly obtained)
           in hex   :         4d1055d0

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

3.generating 100000 N(0,1) random numbers using different engines...
               0 ns used on for-loop body
        14502000 ns for mt19937
        12965000 ns for mt19937_64
        13066000 ns for minstd_rand
        12821000 ns for default_random_engine
       317918000 ns for random_device
stderr
sh: PAUSE: not found