Time difference in C++

2019-01-10 14:31发布

Does anyone know how to calculate time difference in C++ in milliseconds? I used difftime but it doesn't have enough precision for what I'm trying to measure.

标签: c++ time
8条回答
The star\"
2楼-- · 2019-01-10 15:03

if you are using win32 FILETIME is the most accurate that you can get: Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).

So if you want to calculate the difference between two times in milliseconds you do the following:

UINT64 getTime()
{
    SYSTEMTIME st;
    GetSystemTime(&st);

    FILETIME ft;
    SystemTimeToFileTime(&st, &ft);  // converts to file time format
    ULARGE_INTEGER ui;
    ui.LowPart=ft.dwLowDateTime;
    ui.HighPart=ft.dwHighDateTime;

    return ui.QuadPart;
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    //! Start counting time
    UINT64   start, finish;

    start=getTime();

    //do something...

    //! Stop counting elapsed time
    finish = getTime();

    //now you can calculate the difference any way that you want
    //in seconds:
    _tprintf(_T("Time elapsed executing this code: %.03f seconds."), (((float)(finish-start))/((float)10000))/1000 );
    //or in miliseconds
    _tprintf(_T("Time elapsed executing this code: %I64d seconds."), (finish-start)/10000 );
}
查看更多
对你真心纯属浪费
3楼-- · 2019-01-10 15:12

You can get micro and nanosecond precision out of Boost.Date_Time.

查看更多
贪生不怕死
4楼-- · 2019-01-10 15:12

I think you will have to use something platform-specific. Hopefully that won't matter? eg. On Windows, look at QueryPerformanceCounter() which will give you something much better than milliseconds.

查看更多
小情绪 Triste *
5楼-- · 2019-01-10 15:13

I know this is an old question, but there's an updated answer for C++0x. There is a new header called <chrono> which contains modern time utilities. Example use:

#include <iostream>
#include <thread>
#include <chrono>

int main()
{
    typedef std::chrono::high_resolution_clock Clock;
    typedef std::chrono::milliseconds milliseconds;
    Clock::time_point t0 = Clock::now();
    std::this_thread::sleep_for(milliseconds(50));
    Clock::time_point t1 = Clock::now();
    milliseconds ms = std::chrono::duration_cast<milliseconds>(t1 - t0);
    std::cout << ms.count() << "ms\n";
}

50ms

More information can be found here:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2661.htm

There is also now a boost implementation of <chrono>.

查看更多
该账号已被封号
6楼-- · 2019-01-10 15:13

The clock function gives you a millisecond timer, but it's not the greatest. Its real resolution is going to depend on your system. You can try

#include <time.h>

int clo = clock();
//do stuff
cout << (clock() - clo) << endl;

and see how your results are.

查看更多
该账号已被封号
7楼-- · 2019-01-10 15:13

You can use gettimeofday to get the number of microseconds since epoch. The seconds segment of the value returned by gettimeofday() is the same as that returned by time() and can be cast to a time_t and used in difftime. A millisecond is 1000 microseconds.

After you use difftime, calculate the difference in the microseconds field yourself.

查看更多
登录 后发表回答