ViennaCL - The Vienna Computing Library  1.5.0
viennacl/tools/timer.hpp
Go to the documentation of this file.
00001 #ifndef _VIENNACL_TOOLS_TIMER_HPP_
00002 #define _VIENNACL_TOOLS_TIMER_HPP_
00003 
00004 /* =========================================================================
00005    Copyright (c) 2010-2013, Institute for Microelectronics,
00006                             Institute for Analysis and Scientific Computing,
00007                             TU Wien.
00008    Portions of this software are copyright by UChicago Argonne, LLC.
00009 
00010                             -----------------
00011                   ViennaCL - The Vienna Computing Library
00012                             -----------------
00013 
00014    Project Head:    Karl Rupp                   rupp@iue.tuwien.ac.at
00015 
00016    (A list of authors and contributors can be found in the PDF manual)
00017 
00018    License:         MIT (X11), see file LICENSE in the base directory
00019 ============================================================================= */
00020 
00021 
00025 #include <iostream>
00026 
00027 
00028 #ifdef _WIN32
00029 
00030 #define WINDOWS_LEAN_AND_MEAN
00031 #include <windows.h>
00032 #undef min
00033 #undef max
00034 
00035 namespace viennacl{
00036 
00037   namespace tools{
00038 
00043     class timer
00044     {
00045     public:
00046 
00047       timer()
00048       {
00049         QueryPerformanceFrequency(&freq);
00050       }
00051 
00052       void start()
00053       {
00054         QueryPerformanceCounter((LARGE_INTEGER*) &start_time);
00055       }
00056 
00057       double get() const
00058       {
00059         LARGE_INTEGER  end_time;
00060         QueryPerformanceCounter((LARGE_INTEGER*) &end_time);
00061         return (static_cast<double>(end_time.QuadPart) - static_cast<double>(start_time.QuadPart)) / static_cast<double>(freq.QuadPart);
00062       }
00063 
00064 
00065     private:
00066       LARGE_INTEGER freq;
00067       LARGE_INTEGER start_time;
00068     };
00069 
00070   }
00071 
00072 }
00073 
00074 
00075 #else
00076 
00077 #include <sys/time.h>
00078 
00079 namespace viennacl{
00080 
00081   namespace tools{
00082 
00087     class timer
00088     {
00089     public:
00090 
00091       timer() : ts(0)
00092       {}
00093 
00094       void start()
00095       {
00096         struct timeval tval;
00097         gettimeofday(&tval, NULL);
00098         ts = static_cast<double>(tval.tv_sec * 1000000 + tval.tv_usec);
00099       }
00100 
00101       double get() const
00102       {
00103         struct timeval tval;
00104         gettimeofday(&tval, NULL);
00105         double end_time = tval.tv_sec * 1000000 + tval.tv_usec;
00106 
00107         return static_cast<double>(end_time-ts) / 1000000.0;
00108       }
00109 
00110     private:
00111       double ts;
00112     };
00113 
00114   }
00115 
00116 }
00117 
00118 
00119 
00120 #endif
00121 
00122 #endif