codice:
#include <iostream.h>
#ifdef _WIN32
#include <windows.h>
static LARGE_INTEGER _tstart, _tend;
static LARGE_INTEGER freq;
void tstart(void)
{
static int first = 1;
if(first) {
QueryPerformanceFrequency(&freq);
first = 0;
}
QueryPerformanceCounter(&_tstart);
}
void tend(void)
{
QueryPerformanceCounter(&_tend);
}
double tval()
{
return ((double)_tend.QuadPart -
(double)_tstart.QuadPart)/((double)freq.QuadPart);
}
#else
#include <sys/time.h>
static struct timeval _tstart, _tend;
static struct timezone tz;
void tstart(void)
{
gettimeofday(&_tstart, &tz);
}
void tend(void)
{
gettimeofday(&_tend,&tz);
}
double tval()
{
typedef signed long long s;
s t1, t2;
t1 = (s)_tstart.tv_sec + (s)_tstart.tv_usec/(1000*1000);
t2 = (s)_tend.tv_sec + (s)_tend.tv_usec/(1000*1000);
return t2-t1;
}
#endif
int main() {
int c;
cout << "Inserire numero cicli: ";
cin >> c;
tstart();
for(register int i = 0; i < c; i++) {
;
}
tend();
cout << "Il tuo computer ci ha messo: " << tval() << " secondi.\n\n";
cout << "Premi un tasto ed invio per continuare";
char e;
cin >> e;
return 0;
}
questo funziona in windows e forse anche in linux, non ho provato se funzionano i preprocessori, volendo puoi fare quallo che volevi fare nel bat direttamente in C++, prendi l'ora con ctime all'inizio, prendi l'ora alla fine e fai la differenza.