SHOW:
|
|
- or go back to the newest paste.
| 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 = 500000; | |
| 52 | ||
| 53 | Idx zero(0,0,0); | |
| 54 | srand(5); | |
| 55 | START_TIMER(start) | |
| 56 | EIGEN_ASM_COMMENT("BEGIN1");
| |
| 57 | for(auto k=0; k<loops;++k){
| |
| 58 | idx.setRandom(); | |
| 59 | t += zero.max(max.min(idx)); | |
| 60 | } | |
| 61 | EIGEN_ASM_COMMENT("END1");
| |
| 62 | ||
| 63 | STOP_TIMER_MILLI(count,start) | |
| 64 | std::cout << "Eigen3: time: " << count << " ms " << t << std::endl; | |
| 65 | ||
| 66 | t.setZero(); | |
| 67 | srand(5); | |
| 68 | START_TIMER(start2) | |
| 69 | EIGEN_ASM_COMMENT("BEGIN2");
| |
| 70 | for(auto k=0; k<loops;++k){
| |
| 71 | idx.setRandom(); | |
| 72 | t(0) += std::max( std::min( max(0), idx(0)), 0LL ); | |
| 73 | t(1) += std::max( std::min( max(1), idx(1)), 0LL ); | |
| 74 | t(2) += std::max( std::min( max(2), idx(2)), 0LL ); | |
| 75 | } | |
| 76 | STOP_TIMER_MILLI(count2,start2) | |
| 77 | EIGEN_ASM_COMMENT("END2");
| |
| 78 | std::cout << "Seperate: time: " << count2 << " ms " << t << std::endl; | |
| 79 | ||
| 80 | } |