pctimer.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  2. // vim:tabstop=4:shiftwidth=4:expandtab:
  3. /*
  4. * Copyright (C) 2004-2008 Wu Yongwei <adah at users dot sourceforge dot net>
  5. *
  6. * This software is provided 'as-is', without any express or implied
  7. * warranty. In no event will the authors be held liable for any
  8. * damages arising from the use of this software.
  9. *
  10. * Permission is granted to anyone to use this software for any purpose,
  11. * including commercial applications, and to alter it and redistribute
  12. * it freely, subject to the following restrictions:
  13. *
  14. * 1. The origin of this software must not be misrepresented; you must
  15. * not claim that you wrote the original software. If you use this
  16. * software in a product, an acknowledgement in the product
  17. * documentation would be appreciated but is not required.
  18. * 2. Altered source versions must be plainly marked as such, and must
  19. * not be misrepresented as being the original software.
  20. * 3. This notice may not be removed or altered from any source
  21. * distribution.
  22. *
  23. * This file is part of Stones of Nvwa:
  24. * http://sourceforge.net/projects/nvwa
  25. *
  26. */
  27. /*!
  28. * \file pctimer.h
  29. * \ingroup QxMemLeak
  30. *
  31. * Function to get a high-resolution timer for Win32/Cygwin/Unix.
  32. *
  33. * \version 1.6, 2004/08/02
  34. * \author Wu Yongwei
  35. *
  36. */
  37. #ifndef QT_NO_DEBUG
  38. #ifndef _QX_MODE_RELEASE
  39. #if _QX_USE_MEM_LEAK_DETECTION
  40. #ifndef _PCTIMER_H
  41. namespace qx {
  42. namespace memory {
  43. typedef double pctimer_t;
  44. } // namespace memory
  45. } // namespace qx
  46. #if defined(_WIN32) || defined(__CYGWIN__)
  47. #ifndef _WIN32
  48. #define _PCTIMER_NO_WIN32
  49. #endif /* _WIN32 */
  50. #ifndef WIN32_LEAN_AND_MEAN
  51. #define WIN32_LEAN_AND_MEAN
  52. #endif /* WIN32_LEAN_AND_MEAN */
  53. #include <windows.h>
  54. #ifdef _PCTIMER_NO_WIN32
  55. #undef _PCTIMER_NO_WIN32
  56. #undef _WIN32
  57. #endif /* _PCTIMER_NO_WIN32 */
  58. namespace qx {
  59. namespace memory {
  60. __inline pctimer_t pctimer(void)
  61. {
  62. static LARGE_INTEGER __pcount, __pcfreq;
  63. static int __initflag;
  64. if (!__initflag)
  65. {
  66. QueryPerformanceFrequency(&__pcfreq);
  67. __initflag++;
  68. }
  69. QueryPerformanceCounter(&__pcount);
  70. return (double)__pcount.QuadPart / (double)__pcfreq.QuadPart;
  71. }
  72. } // namespace memory
  73. } // namespace qx
  74. #else /* Not Win32/Cygwin */
  75. #include <sys/time.h>
  76. namespace qx {
  77. namespace memory {
  78. __inline pctimer_t pctimer(void)
  79. {
  80. struct timeval __tv;
  81. gettimeofday(&__tv, NULL);
  82. return (double)__tv.tv_sec + (double)__tv.tv_usec / 1000000;
  83. }
  84. } // namespace memory
  85. } // namespace qx
  86. #endif /* Win32/Cygwin */
  87. #endif /* _PCTIMER_H */
  88. #endif // _QX_USE_MEM_LEAK_DETECTION
  89. #endif // _QX_MODE_RELEASE
  90. #endif // QT_NO_DEBUG