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;
}
I am using Ubuntu.
– Avelino
Really need for all these ways? If I don’t sleep I can try to put some of them.
– Maniero
I think I overreacted, haha. It might just be in C++ (with or without Qt, it’s the choice)
– Avelino