(C++, Windows) Simple time counter < ms

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
artsys
Posts: 4
Joined: Sat Mar 04, 2006 5:32 pm

(C++, Windows) Simple time counter < ms

Post by artsys »

#include <iostream>
#include <windows.h>

int main(void)
{
char i;
LARGE_INTEGER Frequency;
LARGE_INTEGER Before, After;
double Time;

/*Note: if this function returns 0, it is the system does not support this functionality*/

QueryPerformanceFrequency(&Frequency);

QueryPerformanceCounter(&Before); // Before !

myfunction(balabala);

QueryPerformanceCounter(&After); // After lol

/* time, in milliseconds */
Time = 1000.0 * (After.QuadPart - Before.QuadPart) / Frequency.QuadPart;

cout<<" Time "<<Time<<endl;
cin >> i;
}
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

Just as a warning, most computers don't support the HighResolutionTimer, at least none that I've come across use it.
Image
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

most computers don't support the HighResolutionTimer
That is crazy. Nearly every PC I've touched in the last 5 years supports the high resolution timer. There are known issues with QueryPerformanceCounter/QueryPerformanceFrequency, but it does 'work'.

BTW, the original posters code is nearly equivalent to

Code: Select all

irr::ITimer* timer = device->getTimer();

u32 before = timer->getRealTime();
// blah blah
u32 after = timer->getRealTime();

u32 milliseconds = after - before;
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Here is a simple high resolution timer test case and the output from my home PC.

Code: Select all

S:\>type t.cpp && cl /nologo /O2 t.cpp && t
#include <windows.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
  LARGE_INTEGER Frequency;
  if (!QueryPerformanceFrequency(&Frequency))
    return fprintf(stderr, "Failed to query performance frequency!\n");

  printf("Freq=%I64u\n", Frequency.QuadPart);

  LARGE_INTEGER Before;
  if (!QueryPerformanceCounter(&Before))
    return fprintf(stderr, "Failed to query performance counter!\n");

  Sleep(2500);

  LARGE_INTEGER After;
  if (!QueryPerformanceCounter(&After))
    return fprintf(stderr, "Failed to query performance counter!\n");

  printf("Elapsed=%f\n",
    1.f * (After.QuadPart - Before.QuadPart) / Frequency.QuadPart);

  int i;
  for (i = 0; i < 20; ++i)
  {
    QueryPerformanceCounter(&Before);
         Sleep(1);
    QueryPerformanceCounter(&After);

    printf("Elapsed=%fs [%I64u counts]\n",
      1.f * (After.QuadPart - Before.QuadPart) / Frequency.QuadPart,
      (After.QuadPart - Before.QuadPart));
  }

  return 0;
}

t.cpp
Freq=3579545
Elapsed=2.495127
Elapsed=0.015235s [54534 counts]
Elapsed=0.015457s [55328 counts]
Elapsed=0.015406s [55146 counts]
Elapsed=0.015509s [55516 counts]
Elapsed=0.015437s [55259 counts]
Elapsed=0.015497s [55471 counts]
Elapsed=0.015443s [55278 counts]
Elapsed=0.015493s [55457 counts]
Elapsed=0.015353s [54957 counts]
Elapsed=0.015468s [55368 counts]
Elapsed=0.015476s [55397 counts]
Elapsed=0.015416s [55182 counts]
Elapsed=0.015484s [55426 counts]
Elapsed=0.015447s [55292 counts]
Elapsed=0.015495s [55466 counts]
Elapsed=0.015453s [55314 counts]
Elapsed=0.015381s [55058 counts]
Elapsed=0.015460s [55339 counts]
Elapsed=0.015468s [55369 counts]
Elapsed=0.015471s [55378 counts]

S:\>
Guest

Post by Guest »

Herr Spintz wrote:
most computers don't support the HighResolutionTimer
That is crazy. Nearly every PC I've touched in the last 5 years supports the high resolution timer. There are known issues with QueryPerformanceCounter/QueryPerformanceFrequency, but it does 'work'.
There's no QueryPerformanceFrequency() on Linux.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

You are correct in saying that Linux doesn't have QueryPerformanceFrequency/QueryPerformanceCounter system calls, but it does support high resolution timers. The gettimeofday call will get you something, and if your system supports the XOPEN realtime extensions you have the clock_gettime function.

All of this is beside the point because the post subject says this topic is C++ and Windows specific... :)

Travis
Post Reply