timingTest.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // timingTest.cpp : Defines the entry point for the console application.
  19. //
  20. #include "stdafx.h"
  21. #define WIN32_LEAN_AND_MEAN
  22. #include <windows.h>
  23. #include <mmsystem.h>
  24. #include <iostream.h>
  25. #include <string.h>
  26. double s_ticksPerSec = 0.0f;
  27. double s_ticksPerMSec = 0.0f;
  28. char buffer[1024];
  29. //-------------------------------------------------------------------------------------------------
  30. void GetPrecisionTimer(INT64* t)
  31. {
  32. // CPUID is needed to force serialization of any previous instructions.
  33. __asm
  34. {
  35. RDTSC
  36. MOV ECX,[t]
  37. MOV [ECX], EAX
  38. MOV [ECX+4], EDX
  39. }
  40. }
  41. //-------------------------------------------------------------------------------------------------
  42. void InitPrecisionTimer()
  43. {
  44. __int64 totalTime = 0;
  45. INT64 TotalTicks = 0;
  46. static int TESTS = 10;
  47. cout << "Starting tests..." << flush;
  48. for (int i = 0; i < TESTS; ++i)
  49. {
  50. int TimeStart;
  51. int TimeStop;
  52. INT64 StartTicks;
  53. INT64 EndTicks;
  54. TimeStart = timeGetTime();
  55. GetPrecisionTimer(&StartTicks);
  56. for(;;)
  57. {
  58. TimeStop = timeGetTime();
  59. if ((TimeStop - TimeStart) > 1000)
  60. {
  61. GetPrecisionTimer(&EndTicks);
  62. break;
  63. }
  64. }
  65. TotalTicks += (EndTicks - StartTicks);
  66. totalTime += (TimeStop - TimeStart);
  67. }
  68. cout << "...completed" << endl;
  69. s_ticksPerMSec = 1.0 * TotalTicks / totalTime;
  70. s_ticksPerSec = s_ticksPerMSec * 1000.0f;
  71. sprintf(buffer, "Ticks per sec: %.2f\n", s_ticksPerSec);
  72. cout << buffer;
  73. sprintf(buffer, "Ticks per msec: %.2f\n", s_ticksPerMSec);
  74. cout << buffer;
  75. }
  76. int main(int argc, char* argv[])
  77. {
  78. INT64 startTime, endTime, totalTime = 0;
  79. InitPrecisionTimer();
  80. FILE *out = fopen("output.txt", "w");
  81. cout << "Beginning Looping tests: " << endl;
  82. const int TESTCOUNT = 60;
  83. while (1) {
  84. for (int i = 0; i < TESTCOUNT; ++i) {
  85. GetPrecisionTimer(&startTime);
  86. Sleep(5);
  87. GetPrecisionTimer(&endTime);
  88. totalTime += (endTime - startTime);
  89. }
  90. double avgPerFrame = 1.0 * totalTime / TESTCOUNT;
  91. sprintf(buffer, "%.8f,\t", avgPerFrame / s_ticksPerMSec );
  92. fwrite(buffer, strlen(buffer), 1, out);
  93. fflush(out);
  94. cout << buffer << endl;
  95. totalTime = 0;
  96. }
  97. fclose(out);
  98. return 0;
  99. }