Created
November 7, 2012 21:32
-
-
Save jesusnoseq/4034632 to your computer and use it in GitHub Desktop.
Clase de C++ para medir el tiempo y suspender un hilo de ejecución con bastante exactitud. Recuerda compilar con -lrt.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef CRONO_HPP | |
#define CRONO_HPP | |
#include <time.h> | |
class Crono | |
{ | |
public: | |
/* | |
Tiempos que se pueden cronometrar: | |
CLOCK_REALTIME | |
CLOCK_MONOTONIC | |
CLOCK_PROCESS_CPUTIME_ID | |
CLOCK_THREAD_CPUTIME_ID | |
*/ | |
Crono(clockid_t clk_id=CLOCK_REALTIME):_clk_id(clk_id) {} | |
void | |
init(clockid_t clk_id) throw() | |
{ | |
setClock(clk_id); | |
clock_gettime(clk_id, &_iniTime); | |
} | |
void | |
init() throw() | |
{ | |
clock_gettime(_clk_id, &_iniTime); | |
} | |
void | |
pause() throw() | |
{ | |
clock_gettime(_clk_id, &_endTime); | |
} | |
clockid_t | |
getClock(clockid_t clk_id) throw() | |
{ | |
return _clk_id; | |
} | |
time_t | |
getSec() throw() | |
{ | |
return diff(_iniTime,_endTime).tv_sec; | |
} | |
unsigned long | |
getMili() throw() | |
{ | |
timespec temp=diff(_iniTime,_endTime); | |
return (long)temp.tv_sec*1000+temp.tv_nsec/1000000; | |
} | |
unsigned long | |
getNano() throw() | |
{ | |
timespec temp=diff(_iniTime,_endTime); | |
return temp.tv_nsec+temp.tv_sec*1000000000; | |
} | |
int | |
nsleep(unsigned long nanosec) throw() | |
{ | |
struct timespec req={0}; | |
time_t sec=(int)(nanosec/1000000000L); | |
nanosec-=sec*1000000000L; | |
req.tv_sec=sec; | |
req.tv_nsec=nanosec; | |
while(nanosleep(&req,&req)); | |
return 0; | |
} | |
int | |
msleep(unsigned long milisec) throw() | |
{ | |
struct timespec req={0}; | |
time_t sec=(int)(milisec/1000); | |
milisec-=sec*1000; | |
req.tv_sec=sec; | |
req.tv_nsec=milisec*1000000L; | |
while(nanosleep(&req,&req)); | |
return 0; | |
} | |
protected: | |
private: | |
void | |
setClock(clockid_t clk_id) | |
{ | |
_clk_id=clk_id; | |
} | |
//from http://www.guyrutenberg.com/2007/09/22/profiling-code-using-clock_gettime/ | |
timespec | |
diff(timespec start, timespec end) throw() | |
{ | |
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; | |
} | |
timespec _iniTime, _endTime; | |
clockid_t _clk_id; | |
}; | |
#endif // CRONO_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// compila con g++ -Wall Crono.hpp cronoPrueba.cc -o pruebaCrono.bin -lrt | |
#include <stdio.h> | |
#include <limits.h> | |
#include <iostream> | |
#include "Crono.hpp" | |
using namespace std; | |
int | |
main (int argc, char* const* argv) | |
{ | |
Crono cronometro; | |
cronometro.init(); | |
cronometro.nsleep(2000000000); | |
cronometro.pause(); | |
cout <<"Tiempo empleado (CLOCK_REALTIME): "<<cronometro.getMili()<<" milisegundos."<<endl; | |
cronometro.init(CLOCK_PROCESS_CPUTIME_ID); | |
cronometro.msleep(5000); | |
cronometro.pause(); | |
cout <<"Tiempo empleado (CLOCK_PROCESS_CPUTIME_ID): "<<cronometro.getNano()<<" nanosegundos."<<endl; | |
cronometro.init(); | |
for(int i=0; i<INT_MAX;i++){} | |
cronometro.pause(); | |
cout <<"Tiempo empleado (CLOCK_PROCESS_CPUTIME_ID): "<<cronometro.getSec()<<" segundos."<<endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment