4

Possible Duplicate:
C++ high precision time measurement in Windows

I am developing a program that downloads files from ftp, and am looking for a High-Res timer library to calculate download speed, for now I am using c++ time(NULL), but results aren't accurate.

Is there a simple/easy to use, plug N play kind of c++ library for windows platform ? Something that gives time elapsed in seconds since last call or something similar.

EDIT:

So the QueryPerformanceCounter() was mentioned many times, but going through other threads this is what I found out :

You should be warned that it is based on the CPU frequency. This frequency is not stable when e.g. a power save mode is enabled. If you want to use this API, make sure the CPU is at a constant frequency.

Be warned, though, that intel's SpeedStep technology may change the PerformanceFrequency without your code noticing

*We also tried fixating the thread affinity for our threads to guarantee that each thread always got a consistent value from QueryPerformanceCounter, this worked but it absolutely killed the performance in the application. *

So considering the situation is it advisable to use it ? The performance of the program and reliablity of the timer is very important

Community
  • 1
  • 1
StudentX
  • 2,243
  • 6
  • 35
  • 67

3 Answers3

3

You have QueryPerformanceCounter provided you don't fall into the buggy use-case described in the remarks on the MSDN docs

Example from: How to use QueryPerformanceCounter?

#include <windows.h>

double PCFreq = 0.0;
__int64 CounterStart = 0;

void StartCounter()
{
    LARGE_INTEGER li;
    if(!QueryPerformanceFrequency(&li))
    cout << "QueryPerformanceFrequency failed!\n";

    PCFreq = double(li.QuadPart)/1000.0;

    QueryPerformanceCounter(&li);
    CounterStart = li.QuadPart;
}
double GetCounter()
{
    LARGE_INTEGER li;
    QueryPerformanceCounter(&li);
    return double(li.QuadPart-CounterStart)/PCFreq;
}

int main()
{
    StartCounter();
    Sleep(1000);
    cout << GetCounter() <<"\n";
    return 0;
}
Community
  • 1
  • 1
Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124
2

If you have a compiler that support C++11, then you can simply use std::chrono, if not then boost::chrono is at your service

BigBoss
  • 6,904
  • 2
  • 23
  • 38
1

Use timeGetTime, the resolution should be sufficient for your needs.

Andreas Brinck
  • 51,293
  • 14
  • 84
  • 114