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

vigra/timing.hxx
00001 /************************************************************************/
00002 /*                                                                      */
00003 /*               Copyright 2008-2009 by Ullrich Koethe                  */
00004 /*                                                                      */
00005 /*    This file is part of the VIGRA computer vision library.           */
00006 /*    The VIGRA Website is                                              */
00007 /*        http://hci.iwr.uni-heidelberg.de/vigra/                       */
00008 /*    Please direct questions, bug reports, and contributions to        */
00009 /*        ullrich.koethe@iwr.uni-heidelberg.de    or                    */
00010 /*        vigra@informatik.uni-hamburg.de                               */
00011 /*                                                                      */
00012 /*    Permission is hereby granted, free of charge, to any person       */
00013 /*    obtaining a copy of this software and associated documentation    */
00014 /*    files (the "Software"), to deal in the Software without           */
00015 /*    restriction, including without limitation the rights to use,      */
00016 /*    copy, modify, merge, publish, distribute, sublicense, and/or      */
00017 /*    sell copies of the Software, and to permit persons to whom the    */
00018 /*    Software is furnished to do so, subject to the following          */
00019 /*    conditions:                                                       */
00020 /*                                                                      */
00021 /*    The above copyright notice and this permission notice shall be    */
00022 /*    included in all copies or substantial portions of the             */
00023 /*    Software.                                                         */
00024 /*                                                                      */
00025 /*    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND    */
00026 /*    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES   */
00027 /*    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND          */
00028 /*    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT       */
00029 /*    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,      */
00030 /*    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      */
00031 /*    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR     */
00032 /*    OTHER DEALINGS IN THE SOFTWARE.                                   */
00033 /*                                                                      */
00034 /************************************************************************/
00035 
00036 
00037 #ifndef VIGRA_TIMING_HXX
00038 #define VIGRA_TIMING_HXX
00039 
00040 #ifndef NDEBUG
00041 
00042 #include <sstream>
00043 
00044 // usage:
00045 // void time_it()
00046 // {
00047 //     USETICTOC;
00048 //     TIC;
00049 //      ...
00050 //     std::cerr << TOC << " for time_it\n";
00051 // }
00052 
00053 #ifdef WIN32
00054 
00055     #include "windows.h"
00056 
00057     namespace {
00058 
00059     inline double queryTimerUnit()
00060     {
00061         LARGE_INTEGER frequency;
00062         QueryPerformanceFrequency(&frequency);
00063         return 1000.0 / frequency.QuadPart;
00064     }
00065 
00066     inline std::string tic_toc_diff(LARGE_INTEGER const & tic)
00067     {
00068         LARGE_INTEGER toc;
00069         QueryPerformanceCounter(&toc);
00070         static double unit = queryTimerUnit();
00071         std::stringstream s;
00072         s << ((toc.QuadPart - tic.QuadPart) * unit) << " msec";
00073         return s.str();
00074     }
00075 
00076     } // unnamed namespace
00077 
00078     #define USETICTOC LARGE_INTEGER tic_timer
00079     #define TIC QueryPerformanceCounter(&tic_timer)
00080     #define TOC tic_toc_diff(tic_timer)
00081 
00082 #else
00083 
00084     #if defined(VIGRA_HIRES_TIMING) && !defined(__CYGWIN__)
00085         // requires linking against librt
00086     
00087         #include <time.h>
00088 
00089         namespace {
00090 
00091         inline std::string tic_toc_diff(timespec const & tic)
00092         {
00093             timespec toc;
00094             clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &toc);
00095             std::stringstream s;
00096             s << ((toc.tv_sec*1000.0 + toc.tv_nsec/1000000.0) -
00097                   (tic.tv_sec*1000.0 + tic.tv_nsec/1000000.0)) << " msec";
00098             return s.str();
00099         }
00100 
00101         } // unnamed namespace
00102 
00103         #define USETICTOC timespec tic_timer
00104         #define TIC clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tic_timer)
00105         #define TOC tic_toc_diff(tic_timer)
00106 
00107     #else
00108     
00109         #include <sys/time.h>
00110 
00111         namespace {
00112 
00113         inline std::string tic_toc_diff(timeval const & tic)
00114         {
00115             timeval toc;
00116             gettimeofday(&toc, NULL);
00117             std::stringstream s;
00118             s << ((toc.tv_sec*1000.0 + toc.tv_usec/1000.0) -
00119                   (tic.tv_sec*1000.0 + tic.tv_usec/1000.0)) << " msec";
00120             return s.str();
00121         }
00122 
00123         } // unnamed namespace
00124 
00125         #define USETICTOC timeval tic_timer
00126         #define TIC gettimeofday(&tic_timer, NULL)
00127         #define TOC tic_toc_diff(tic_timer)
00128 
00129     #endif // VIGRA_HIRES_TIMING
00130 
00131 #endif // WIN32
00132 
00133 #else // NDEBUG
00134 
00135 #define USETICTOC 
00136 #define TIC 
00137 #define TOC
00138 
00139 #endif // NDEBUG
00140 
00141 #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.0 (Thu Aug 25 2011)