doblespiel/timer.c

97 lines
2.0 KiB
C

#define _POSIX_C_SOURCE 199309L
#include "timer.h"
#include <stdio.h>
#include <time.h>
/*#if __APPLE__
#include <sys/time.h>
static struct timespec start = {0, 0};
// Starts the timer.
void startTimer()
{
clock_gettime(CLOCK_MONOTONIC, &start);
}
// Returns the time in seconds since startTimer() was called.
double stopTimer()
{
struct timespec end;
clock_gettime(CLOCK_MONOTONIC, &end);
unsigned long long delta_us = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000;
double measuredSeconds = (double)delta_us / 1000000.;
if(start.tv_nsec > 0) {
start.tv_nsec = 0;
start.tv_sec = 0;
}
else
measuredSeconds = -1;
return measuredSeconds;
}
#else
#include <time.h>
static clock_t startClocks = 0;
// Starts the timer.
void startTimer()
{
startClocks = clock();
printf("\nstartclocks: %ld\n", startClocks);
}
// Returns the time in seconds since startTimer() was called.
double stopTimer()
{
clock_t endClocks = clock();
printf("\nendclocks: %ld\n", endClocks);
printf("\ndeltatimer: %ld\n", (endClocks - startClocks));
printf("CLOCKS_PER_SECONDS: %ld\n", CLOCKS_PER_SEC);
double measuredSeconds = (double)(endClocks - startClocks) / CLOCKS_PER_SEC;
if(startClocks > 0)
startClocks = 0;
else
measuredSeconds = -1;
printf("measuredsecondes: %f\n", measuredSeconds);
return measuredSeconds;
}
#endif*/
static struct timespec start;
void startTimer() {
clock_gettime(CLOCK_MONOTONIC, &start);
}
static struct timespec end;
double stopTimer() {
clock_gettime(CLOCK_MONOTONIC, &end);
double measuredseconds = (double)(end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) * 0.000000001;
if (start.tv_sec>0 || start.tv_nsec>0){
end.tv_nsec = 0;
end.tv_sec =0;
start.tv_sec = 0;
start.tv_nsec = 0;
}else {
measuredseconds = -1;
}
return measuredseconds;
}