This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

What is the c++11 steady_clock measuring?


Hello,

I would like to measure the performance of the GCC c++11 Poisson random
number generator, but the in-program measurement of time intervals is
confusing me. I have two pieces of code (below): in the first I use the
ctime clock() function and get sensible results, in the second I use
std::chrono:steady_clock and get results that I think are wrong. Could
somebody please tell me what I'm doing wrong. I use g++ (GCC) 4.8.0 on a
Linux X.X.X 2.6.32-358.0.1.el6.x86_64 #1 SMP Tue Feb 26 14:48:51 CST
2013 x86_64 x86_64 x86_64 GNU/Linux system.

BR,
Henrik Mannerström


***** Typical output of first program *****
>g++ -Wall -Wextra -pedantic -std=c++11 -O2 test7.cc -o test7
>time ./test7
C++11 library RNG - C clock
4999561509
Total 7.86 s
786 ns/rnv

real    0m7.870s
user    0m7.864s
sys     0m0.002s



***** Typical output of second program *****
>g++ -Wall -Wextra -pedantic -std=c++11 -O2 test5.cc -o test5
>time ./test5
C++11 library RNG - steady_clock
5000562918
Total 3.59925 s
359.925 ns/rnv

real    0m7.900s
user    0m7.894s
sys     0m0.002s



***** First file *****
#include
<iostream>                                                                                                                                                                                                                         

#include
<random>                                                                                                                                                                                                                           

#include
<ctime>                                                                                                                                                                                                                            

                                                                                                                                                                                                                                            

unsigned int const sz =
10000000;                                                                                                                                                                                                           

                                                                                                                                                                                                                                            

int
main()                                                                                                                                                                                                                                  

{                                                                                                                                                                                                                                           

  std::random_device
rd;                                                                                                                                                                                                                    

  std::mt19937
gen(rd());                                                                                                                                                                                                                   

  std::uniform_real_distribution<> unif(0,
1e3);                                                                                                                                                                                            

                                                                                                                                                                                                                                            

  int unsigned long sum =
0;                                                                                                                                                                                                                

  clock_t start =
clock();                                                                                                                                                                                                                  

  for (unsigned int k=0;k!=sz;k+=1)
{                                                                                                                                                                                                       

    std::poisson_distribution<unsigned int>
poiss(unif(gen));                                                                                                                                                                               

    sum +=
poiss(gen);                                                                                                                                                                                                                      

 
}                                                                                                                                                                                                                                         

  double elapsed_seconds = static_cast<double>(clock() - start) /
static_cast<double>(CLOCKS_PER_SEC);                                                                                                                                      

                                                                                                                                                                                                                                            

  std::cout << "C++11 library RNG - C clock" <<
std::endl;                                                                                                                                                                                  

  std::cout << sum <<
std::endl;                                                                                                                                                                                                            

  std::cout << "Total " << elapsed_seconds << " s" <<
std::endl;                                                                                                                                                                            

  std::cout << 1e9 * elapsed_seconds / static_cast<double>(sz) << "
ns/rnv" <<
std::endl;                                                                                                                                                   

}


***** Second file *****
#include
<iostream>                                                                                                                                                                                                                         

#include
<random>                                                                                                                                                                                                                           

#include
<chrono>                                                                                                                                                                                                                           

                                                                                                                                                                                                                                            

unsigned int const sz =
10000000;                                                                                                                                                                                                           

                                                                                                                                                                                                                                            

typedef std::chrono::steady_clock
mclock;                                                                                                                                                                                                   

                                                                                                                                                                                                                                            

int
main()                                                                                                                                                                                                                                  

{                                                                                                                                                                                                                                           

  std::chrono::time_point<mclock> start,
end;                                                                                                                                                                                               

                                                                                                                                                                                                                                            

  std::random_device
rd;                                                                                                                                                                                                                    

  std::mt19937
gen(rd());                                                                                                                                                                                                                   

  std::uniform_real_distribution<> unif(0,
1e3);                                                                                                                                                                                            

                                                                                                                                                                                                                                            

  int unsigned long sum =
0;                                                                                                                                                                                                                

  start =
mclock::now();                                                                                                                                                                                                                    

  for (unsigned int k=0;k!=sz;k+=1)
{                                                                                                                                                                                                       

    std::poisson_distribution<unsigned int>
poiss(unif(gen));                                                                                                                                                                               

    sum +=
poiss(gen);                                                                                                                                                                                                                      

 
}                                                                                                                                                                                                                                         

  end =
mclock::now();                                                                                                                                                                                                                      

                                                                                                                                                                                                                                            

  int unsigned elapsed_nseconds =
std::chrono::duration_cast<std::chrono::nanoseconds>                                                                                                                                                      

   
(end-start).count();                                                                                                                                                                                                                    

                                                                                                                                                                                                                                            

  std::cout << "C++11 library RNG - steady_clock" <<
std::endl;                                                                                                                                                                             

  std::cout << sum <<
std::endl;                                                                                                                                                                                                            

  std::cout << "Total " << static_cast<double>(elapsed_nseconds)/1e9 <<
" s" <<
std::endl;                                                                                                                                                  

  std::cout << static_cast<double>(elapsed_nseconds) /
static_cast<double>(sz) << " ns/rnv" <<
std::endl;                                                                                                                                   

}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]