66 lines
1.7 KiB
C++
66 lines
1.7 KiB
C++
#include "Crono.h"
|
|
|
|
// agrupación de clases, funciones y variables globales
|
|
namespace ss {
|
|
|
|
Crono::Crono() // constructor
|
|
{
|
|
// Paso 1: Obtiene la frecuencia del timer del PC en tics/segundo usando QueryPerformanceFrequency
|
|
if (_ticsPorSegundoDelTimerDelPC < 0) {
|
|
// No se ha calculado la frecuencia aún. Se calcula ahora
|
|
LARGE_INTEGER frecuencia;
|
|
char _cadDbg[TAM_CADENA_DEBUG]; // snprintf almacena en una matriz intermedia y al pasar
|
|
// _cadDbg es un puntero a _cadDbg[0]
|
|
|
|
if (QueryPerformanceFrequency(&frecuencia)) {
|
|
_ticsPorSegundoDelTimerDelPC = frecuencia.QuadPart;
|
|
|
|
snprintf(_cadDbg, sizeof(_cadDbg), "La frecuencia del contador del PC es %d Hz\n",
|
|
_ticsPorSegundoDelTimerDelPC);
|
|
OutputDebugStringA(_cadDbg);
|
|
}
|
|
else {
|
|
OutputDebugStringA("Error al ejecutar QueryPerformanceFrequency en " __FUNCTION__);
|
|
}
|
|
}
|
|
}
|
|
|
|
Crono::~Crono() // destructor
|
|
{
|
|
}
|
|
|
|
bool Crono::Inicio()
|
|
{
|
|
bool res = false;
|
|
|
|
if (_ticsPorSegundoDelTimerDelPC > 0) {
|
|
// Paso 2: guarda el valor actual del timer del PC en tics usando QueryPerformanceCounter
|
|
LARGE_INTEGER ticks;
|
|
if (QueryPerformanceCounter(&ticks)) {
|
|
_ticsAntes = ticks.QuadPart;
|
|
res = true;
|
|
}
|
|
else {
|
|
OutputDebugStringA("Error al ejecutar QueryPerformanceCounter en " __FUNCTION__);
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
|
|
double Crono::Lee()
|
|
{
|
|
double segundos = -1.0;
|
|
|
|
// Paso 3: Obtiene el valor actual del timer del PC en tics usando QueryPerformanceCounter
|
|
LARGE_INTEGER ticks;
|
|
if (QueryPerformanceCounter(&ticks)) {
|
|
int64_t ahora = ticks.QuadPart;
|
|
segundos = (double)(ahora - _ticsAntes) / _ticsPorSegundoDelTimerDelPC;
|
|
}
|
|
else {
|
|
printf("Error al ejecutar QueryPerformanceCounter en " __FUNCTION__);
|
|
}
|
|
return segundos;
|
|
}
|
|
|
|
} |