How to get the operating system time in nanoseconds or milliseconds using Qt Creator?

3

How can I get system time in nanoseconds or milliseconds in C++ using Qt Creator ?

For example, in% use% use:

long tempoInicial = System.nanoTime();
treinaRNAEpocas(10000);
long tempoFinal = System.nanoTime();
long tempoDecorridoNs = tempoInicial - tempoFinal;

or even:

long tempoInicial = System.currentTimeMillis();
treinaRNAEpocas(10000);
long tempoFinal = System.currentTimeMillis();
long tempoDecorridoMs = tempoInicial - tempoFinal;
    
asked by anonymous 20.03.2015 / 05:46

2 answers

2

Using Qt

Here is an adapted example of Qt's own Qt documentation :

QTime t;
t.start(); // Aqui inicializamos a contagem

// seu código
treinaRNAEpocas(10000);

//resultado da medida
qDebug( "Tempo decorrido: %d milissegundos", t.elapsed() );

Note that QTime is suitable for measurements of up to 24 hours, and how it makes use of system clock is limited to tests that do not match clock settings (manually or automatically).

Accuracy depends heavily on the OS.


C ++ on Linux

You can use clock_gettime() , which has good precision, as the example removed this blog :

#include <iostream>
#include <time.h>
using namespace std;

timespec diff(timespec start, timespec end);

int main() {
    timespec time1, time2;
    int temp;
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
    // seu código vai aqui /////////////////
    treinaRNAEpocas(10000);
    ////////////////////////////////////////
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
    cout<<diff(time1,time2).tv_sec<<":"<<diff(time1,time2).tv_nsec<<endl;
    return 0;
}

timespec diff(timespec start, timespec end) {
    timespec temp;
    if ((end.tv_nsec-start.tv_nsec)<0) {
        temp.tv_sec = end.tv_sec-start.tv_sec-1;
        temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
    } else {
        temp.tv_sec = end.tv_sec-start.tv_sec;
        temp.tv_nsec = end.tv_nsec-start.tv_nsec;
    }
    return temp;
}


and for Windows

This solution was found in this answer of "SOzão".

#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();
    // seu código vai aqui /////////////////
    treinaRNAEpocas(10000);
    ////////////////////////////////////////
    cout << GetCounter() <<"\n";
    return 0;
}
    
20.03.2015 / 07:39
1

Only one add-on (this is actually a formatted comment):

The clock_gettime() function has great accuracy. Although in the present case it probably does not take even microseconds (the last neural network I've been working with, it was taking 3 days to train), regarding the accuracy of clock_gettime , I just wanted to remember that there is another associated with this function: / p>

clock_getres(...)

This allows you to get the precision / resolution of the value returned by clock_gettime() .

The resolution / precision of the clocks depends on the implementation (commitment between consumption and accuracy)

    
25.03.2015 / 11:19