Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.5 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996.
  5. //
  6. // File: B E N C H M R K . C P P
  7. //
  8. // Contents: Benchmarking class
  9. //
  10. // Notes:
  11. //
  12. // Author: billbe 13 Oct 1997
  13. //
  14. //---------------------------------------------------------------------------
  15. #include <pch.h>
  16. #pragma hdrstop
  17. #include "benchmrk.h"
  18. CBenchmark::CBenchmark()
  19. : m_i64Frequency(1000),
  20. m_sznDescription(NULL),
  21. m_i64TotalTime(0),
  22. m_fStarted(FALSE)
  23. {
  24. LARGE_INTEGER li1;
  25. // Check if QueryPerformanceCounter is supported
  26. if (QueryPerformanceCounter(&li1))
  27. {
  28. // Now get # of ticks per second
  29. QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>
  30. (&m_i64Frequency));
  31. m_fSupported = TRUE;
  32. }
  33. else
  34. {
  35. TraceTag(ttidBenchmark, "High performance counter is not supported.");
  36. m_fSupported = FALSE;
  37. }
  38. }
  39. CBenchmark::~CBenchmark()
  40. {
  41. delete [] m_sznDescription;
  42. }
  43. void
  44. CBenchmark::Start(PCSTR sznDescription)
  45. {
  46. // If QueryPerformanceCounter is supported
  47. if (m_fSupported)
  48. {
  49. // delete the old description
  50. delete [] m_sznDescription;
  51. // replace with new one if specified
  52. if (sznDescription)
  53. {
  54. m_sznDescription = new CHAR[strlen(sznDescription) + 1];
  55. if (m_sznDescription)
  56. {
  57. strcpy(m_sznDescription, sznDescription);
  58. }
  59. }
  60. else
  61. {
  62. // no description specified clear the member variable
  63. m_sznDescription = NULL;
  64. }
  65. m_fStarted = TRUE;
  66. m_i64TotalTime = 0;
  67. // Record our start time
  68. QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>
  69. (&m_i64StartTime));
  70. }
  71. }
  72. void
  73. CBenchmark::Stop()
  74. {
  75. __int64 i64Stop;
  76. // Record our stop time
  77. QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&i64Stop));
  78. // If start was called prior to stop, then record the total time and
  79. // reset our m_fStarted flag
  80. //
  81. if (m_fStarted)
  82. {
  83. m_fStarted = FALSE;
  84. m_i64TotalTime = i64Stop - m_i64StartTime;
  85. }
  86. else
  87. {
  88. // invalidate previous benchmark since stop was called before start
  89. m_i64TotalTime = 0;
  90. }
  91. }
  92. PCSTR
  93. CBenchmark::SznBenchmarkSeconds(unsigned short usPrecision)
  94. {
  95. CHAR sznFmt[10];
  96. sprintf(sznFmt, "%%.%df", usPrecision);
  97. sprintf(m_sznSeconds, sznFmt, DblBenchmarkSeconds());
  98. return m_sznSeconds;
  99. }