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

Asked

Viewed 1,296 times

3

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

For example, in Java 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;
  • I am using Ubuntu.

  • Really need for all these ways? If I don’t sleep I can try to put some of them.

  • 1

    I think I overreacted, haha. It might just be in C++ (with or without Qt, it’s the choice)

2 answers

2


Using the Qt

Follows an example adapted from the 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 the Qtime is suitable for measures up to 24 hours, and as it makes use of the system clock, is limited to tests that do not match the clock settings (manually or automatically).

Accuracy depends heavily on OS.


C++ on Linux

You can use the clock_gettime(), that has good accuracy, as the example taken of 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 has been 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;
}
  • Thank you, this will do. I want to measure the training time of a neural network. :)

  • @Avelino, To measure training times of neural nets, you don’t need nanoseconds! (just a kitchen clock:)

  • @Jjoao I know. But I decided to ask as a curiosity (I may need this one day) and I am measuring the time for each neuron too.

  • @Avelino, of course, I understood, and studying these subjects is always relevant and suspicious!

1

Just an add-on (actually this is a formatted comment):

the function clock_gettime() has great precision. Although in the present case it is probably not necessary even microseconds (the last neuronal network I was in, was taking 3 days to train), in relation to the accuracy of clock_gettime I just wanted to remind that there is associated with this function another:

clock_getres(...)

that allows to obtain the accuracy / resolution of the value returned by clock_gettime().

The resolution / accuracy of clocks, depends on the implementation (compromise between consumption and accuracy)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.