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;
}