[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

vigra/timing.hxx
00001 /************************************************************************/
00002 /*                                                                      */
00003 /*               Copyright 2008-2009 by Ullrich Koethe                  */
00004 /*       Cognitive Systems Group, University of Hamburg, Germany        */
00005 /*                                                                      */
00006 /*    This file is part of the VIGRA computer vision library.           */
00007 /*    The VIGRA Website is                                              */
00008 /*        http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/      */
00009 /*    Please direct questions, bug reports, and contributions to        */
00010 /*        ullrich.koethe@iwr.uni-heidelberg.de    or                    */
00011 /*        vigra@informatik.uni-hamburg.de                               */
00012 /*                                                                      */
00013 /*    Permission is hereby granted, free of charge, to any person       */
00014 /*    obtaining a copy of this software and associated documentation    */
00015 /*    files (the "Software"), to deal in the Software without           */
00016 /*    restriction, including without limitation the rights to use,      */
00017 /*    copy, modify, merge, publish, distribute, sublicense, and/or      */
00018 /*    sell copies of the Software, and to permit persons to whom the    */
00019 /*    Software is furnished to do so, subject to the following          */
00020 /*    conditions:                                                       */
00021 /*                                                                      */
00022 /*    The above copyright notice and this permission notice shall be    */
00023 /*    included in all copies or substantial portions of the             */
00024 /*    Software.                                                         */
00025 /*                                                                      */
00026 /*    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND    */
00027 /*    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES   */
00028 /*    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND          */
00029 /*    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT       */
00030 /*    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,      */
00031 /*    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      */
00032 /*    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR     */
00033 /*    OTHER DEALINGS IN THE SOFTWARE.                                   */
00034 /*                                                                      */
00035 /************************************************************************/
00036 
00037 
00038 #ifndef VIGRA_TIMING_HXX
00039 #define VIGRA_TIMING_HXX
00040 #ifndef VIGRA_NO_TIMING
00041 
00042 #include <iostream>
00043 #include <sstream>
00044 #ifdef MULTI_TICTOC
00045     #include <vector>
00046 #endif
00047 // usage:
00048 // void time_it()
00049 // {
00050 //     USETICTOC;
00051 //     TIC;
00052 //      ...
00053 //     std::cerr << TOC << " for time_it\n";
00054 // }
00055 
00056 #ifdef WIN32
00057 
00058     #include "windows.h"
00059 
00060     namespace {
00061 
00062     inline double queryTimerUnit()
00063     {
00064         LARGE_INTEGER frequency;
00065         QueryPerformanceFrequency(&frequency);
00066         return 1000.0 / frequency.QuadPart;
00067     }
00068 
00069     inline double tic_toc_diff_num(LARGE_INTEGER const & tic)
00070     {
00071         LARGE_INTEGER toc;
00072         QueryPerformanceCounter(&toc);
00073         static double unit = queryTimerUnit();
00074         return ((toc.QuadPart - tic.QuadPart) * unit);
00075     }
00076 
00077     inline std::string tic_toc_diff_string(LARGE_INTEGER const & tic)
00078     {
00079         double diff = tic_toc_diff_num(tic); 
00080         std::stringstream s;
00081         s << diff << " msec";
00082         return s.str();
00083     }
00084 
00085     inline void tic_toc_diff(LARGE_INTEGER const & tic)
00086     {
00087         std::cerr << tic_toc_diff_string(tic) <<std::endl;
00088     }
00089 
00090     } // unnamed namespace
00091     
00092 #ifndef MULTI_TICTOC
00093     #define USETICTOC LARGE_INTEGER tic_timer;
00094     #define TIC QueryPerformanceCounter(&tic_timer);
00095     #define TOC  tic_toc_diff       (tic_timer);
00096     #define TOCN tic_toc_diff_num   (tic_timer);
00097     #define TOCS tic_toc_diff_string(tic_timer);
00098 #else
00099     #define USETICTOC std::vector<LARGE_INTEGER> tic_timer;
00100     #define TIC tic_timer.push_back(LARGE_INTEGER());\
00101                 QueryPerformanceCounter(&(tic_timer.back()));
00102     #define TOC  tic_toc_diff       (tic_timer.back());\
00103                  tic_timer.pop_back();
00104     #define TOCN tic_toc_diff_num   (tic_timer.back());\
00105                  tic_timer.pop_back();
00106     #define TOCS tic_toc_diff_string(tic_timer.back());\
00107                  tic_timer.pop_back();
00108 #endif
00109 
00110 #else
00111 
00112     #if defined(VIGRA_HIRES_TIMING) && !defined(__CYGWIN__)
00113         // requires linking against librt
00114     
00115         #include <time.h>
00116 
00117         namespace {
00118 
00119         inline double tic_toc_diff_num(timespec const & tic)
00120         {
00121             timespec toc;
00122             clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &toc);
00123             return ((toc.tv_sec*1000.0 + toc.tv_nsec/1000000.0) -
00124                   (tic.tv_sec*1000.0 + tic.tv_nsec/1000000.0));
00125         }
00126 
00127         inline std::string tic_toc_diff_string(timeval const & tic)
00128         {
00129             double diff = tic_toc_diff_num(tic); 
00130             std::stringstream s;
00131             s << diff << " msec";
00132             return s.str();
00133         }
00134 
00135         inline void tic_toc_diff(timeval const & tic)
00136         {
00137             std::cerr << tic_toc_diff_string(tic) << std::endl;
00138         }
00139         } // unnamed namespace
00140 
00141 #ifndef MULTI_TICTOC
00142         #define USETICTOC timespec tic_timer;
00143         #define TIC clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tic_timer);
00144         #define TOC  tic_toc_diff       (tic_timer);
00145         #define TOCN tic_toc_diff_num   (tic_timer);
00146         #define TOCS tic_toc_diff_string(tic_timer);
00147 #else
00148 
00149         #define USETICTOC std::vector<timespec> tic_timer;
00150         #define TIC tic_timer.push_back(timespec());\
00151                     clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(tic_timer.back()));
00152         #define TOC  tic_toc_diff       (tic_timer.back());\
00153                      tic_timer.pop_back();
00154         #define TOCN tic_toc_diff_num   (tic_timer.back());\
00155                      tic_timer.pop_back();
00156         #define TOCS tic_toc_diff_string(tic_timer.back());\
00157                      tic_timer.pop_back();
00158 #endif
00159     #else
00160     
00161         #include <sys/time.h>
00162 
00163         namespace {
00164 
00165         inline double tic_toc_diff_num(timeval const & tic)
00166         {
00167             timeval toc;
00168             gettimeofday(&toc, NULL);
00169             return  ((toc.tv_sec*1000.0 + toc.tv_usec/1000.0) -
00170                         (tic.tv_sec*1000.0 + tic.tv_usec/1000.0));
00171         }
00172         inline std::string tic_toc_diff_string(timeval const & tic)
00173         {
00174             double diff = tic_toc_diff_num(tic); 
00175             std::stringstream s;
00176             s << diff << " msec";
00177             return s.str();
00178         }
00179         inline void tic_toc_diff(timeval const & tic)
00180         {
00181             std::cerr << tic_toc_diff_string(tic)<< std::endl;
00182         }
00183 
00184         } // unnamed namespace
00185 
00186 #ifndef MULTI_TICTOC
00187         #define USETICTOC timeval tic_timer;
00188         #define TIC  gettimeofday       (&tic_timer, NULL);
00189         #define TOC  tic_toc_diff       (tic_timer);
00190         #define TOCN tic_toc_diff_num   (tic_timer);
00191         #define TOCS tic_toc_diff_string(tic_timer);
00192 #else
00193 
00194         #define USETICTOC std::vector<timeval> tic_timer;
00195         #define TIC tic_timer.push_back(timeval());\
00196                     gettimeofday(&(tic_timer.back()), NULL);
00197         #define TOC  tic_toc_diff       (tic_timer.back());\
00198                      tic_timer.pop_back();
00199         #define TOCN tic_toc_diff_num   (tic_timer.back());\
00200                      tic_timer.pop_back();
00201         #define TOCS tic_toc_diff_string(tic_timer.back());\
00202                      tic_timer.pop_back();
00203 #endif
00204 
00205     #endif // VIGRA_HIRES_TIMING
00206 
00207 #endif // WIN32
00208 
00209 
00210 
00211 
00212 #else // NDEBUG
00213 
00214 #define USETICTOC 
00215 #define TIC
00216 #define TOC
00217 #define TOCN
00218 #define TICS
00219 #endif // NDEBUG
00220 
00221 #endif // VIGRA_TIMING_HXX

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de)
Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany

html generated using doxygen and Python
vigra 1.7.1 (Sun Feb 19 2012)