home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / include / k3d / k3dsdk / high_res_timer.h < prev    next >
C/C++ Source or Header  |  2008-01-23  |  3KB  |  141 lines

  1. #ifndef K3DSDK_HIGH_RES_TIMER_H
  2. #define K3DSDK_HIGH_RES_TIMER_H
  3.  
  4. // K-3D
  5. // Copyright (c) 1995-2004, Timothy M. Shead
  6. //
  7. // Contact: tshead@k-3d.com
  8. //
  9. // This program is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. // General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public
  20. // License along with this program; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22.  
  23. /** \file
  24.         \brief Declares timer classes for performance tuning
  25.         \author Ed Millard (emillard@direcway.com)
  26. */
  27.  
  28. #include "k3d-platform-config.h"
  29.  
  30. #include "result.h"
  31.  
  32. #include <boost/utility.hpp>
  33. #include <iostream>
  34. #include <string>
  35.  
  36.  
  37. #ifdef K3D_API_WIN32
  38.     #include "win32.h"
  39. #else
  40.     #include <sys/time.h>
  41. #endif // K3D_API_WIN32
  42.  
  43. namespace k3d
  44. {
  45.  
  46. ////////////////////////////////////////////////////////////////////////////////////////////////
  47. // nanotime
  48.  
  49. /* Un-comment this if you require more accurate timing for profiling purposes
  50.  
  51. /// Retrieves a timestamp using an x86-specific CPU counter.  Note that because we are dividing by a fixed number instead of the actual CPU frequency, the result doesn't represent real-world units, but it can still be used to obtain accurate relative timing for profiling.
  52. inline double nanotime()
  53. {
  54. #if defined K3D_HAVE_X86
  55.  
  56.     unsigned long long int val;
  57.     __asm__ __volatile__("rdtsc" : "=A" (val) : );
  58.     return static_cast<double>(val) / 1000000000.0;
  59. #endif
  60. }
  61.  
  62. */
  63.  
  64. #ifdef K3D_API_WIN32
  65.  
  66. /// Retrieves a timestamp in seconds using the Win32 high performance counters
  67. inline double nanotime()
  68. {
  69.     LARGE_INTEGER timestamp;
  70.     LARGE_INTEGER frequency;
  71.     return_val_if_fail(QueryPerformanceCounter(×tamp) && QueryPerformanceFrequency(&frequency), 0.0);
  72.  
  73.     return static_cast<double>(timestamp.QuadPart) / static_cast<double>(frequency.QuadPart);
  74. }
  75.  
  76. #else // K3D_API_WIN32
  77.  
  78. /// Retrieves a timestamp in seconds using gettimeofday() for portable timing
  79. inline double nanotime()
  80. {
  81.     timeval tv;
  82.     gettimeofday(&tv, 0);
  83.  
  84.     return tv.tv_sec + static_cast<double>(tv.tv_usec) / 1000000;
  85. }
  86.  
  87. #endif // !K3D_API_WIN32
  88.  
  89. ////////////////////////////////////////////////////////////////////////////////////////////////
  90. // timer
  91.  
  92. /// Measures elapsed time using a high-resolution timer.  Intentionally modelled on boost::timer
  93. class timer
  94. {
  95. public:
  96.     timer() :
  97.         m_start(nanotime())
  98.     {
  99.     }
  100.     
  101.     void restart()
  102.     {
  103.         m_start = nanotime();
  104.     }
  105.     
  106.     double elapsed() const
  107.     {
  108.         return nanotime() - m_start;
  109.     }
  110.     
  111. private:
  112.     double m_start;
  113. };
  114.  
  115. /////////////////////////////////////////////////////////////////////////////////////////////////
  116. // progress_timer
  117.  
  118. /// Measures elapsed time using a high-resolution timer, printing the results to a stream on destruction.  Intentionally modelled on boost::progress_timer
  119. class progress_timer :
  120.     public timer,
  121.     public boost::noncopyable
  122. {
  123. public:
  124.     progress_timer(std::ostream& Stream = log()) :
  125.         m_stream(Stream)
  126.     {
  127.     }
  128.     
  129.     ~progress_timer()
  130.     {
  131.         m_stream << elapsed() << " s\n";
  132.     }
  133.  
  134. private:
  135.     std::ostream& m_stream;
  136. };
  137.  
  138. } // namespace k3d
  139.  
  140. #endif // !K3DSDK_HIGH_RES_TIMER_H
  141.