Advertisement
Guest User

Untitled

a guest
Oct 5th, 2015
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #include <chrono>
  2. #include <Eigen/Dense>
  3.  
  4.  
  5. #define START_TIMER(start)  auto start = std::chrono::high_resolution_clock::now();
  6.  
  7. #define STOP_TIMER_MILLI( _count_, start)  \
  8.     auto _count_ = std::chrono::duration<double,std::milli>(std::chrono::high_resolution_clock::now() - start ).count();
  9.  
  10. class CPUTimer{
  11.  
  12. public:
  13.     using ClockType = std::chrono::high_resolution_clock;
  14.  
  15.     inline void start(){
  16.         m_start = ClockType::now();
  17.     }
  18.  
  19.     /** returns elapsed time in nanosecond*/
  20.     inline double elapsed(){
  21.         return std::chrono::duration<double,std::nano >(ClockType::now()-m_start).count();
  22.     }
  23.     /** returns elapsed time in second*/
  24.     inline double elapsedSec(){
  25.         return std::chrono::duration<double,std::ratio<1> >(ClockType::now()-m_start).count();
  26.     }
  27.     /** returns elapsed time in minutes*/
  28.     inline double elapsedMin(){
  29.         return std::chrono::duration<double,std::ratio<60> >(ClockType::now()-m_start).count();
  30.     }
  31.  
  32.     /** returns elapsed time in millisecond*/
  33.     inline double elapsedMilliSec(){
  34.          return std::chrono::duration<double,std::milli>(ClockType::now()-m_start).count();
  35.     }
  36.  
  37. private:
  38.     std::chrono::time_point<ClockType> m_start;
  39. };
  40.  
  41. int  main( int  argc, char  ** argv ){
  42.  
  43.  
  44.  
  45.     using Idx = Eigen::Array<long long int,3,1>  ;
  46.  
  47.     Idx max(4,3,9);
  48.     Idx idx(-1,5,10);
  49.     Idx t(0,0,0);
  50.  
  51.     auto loops = 100000UL;
  52.  
  53.     START_TIMER(start)
  54.     Idx zero(0,0,0);
  55.     for(auto k=0; k<loops;++k){
  56.         t += zero.max(max.min(idx));
  57.     }
  58.     STOP_TIMER_MILLI(count,start)
  59.     std::cout << "Eigen3: time: " << count << " ms " << t <<  std::endl;
  60.  
  61.     t.setZero();
  62.     START_TIMER(start2)
  63.     for(auto k=0; k<loops;++k){
  64.     t(0) += std::max(   std::min( max(0), idx(0)),  0LL   );
  65.     t(1) += std::max(   std::min( max(1), idx(1)),  0LL   );
  66.     t(2) += std::max(   std::min( max(2), idx(2)),  0LL   );
  67.     }
  68.     STOP_TIMER_MILLI(count2,start2)
  69.     std::cout << "Seperate: time: " << count2 << " ms " << t << std::endl;
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement