#include <time.h> // clock(), clock_t
#include <iostream> // cout
typedef clock_t *reloj;
constdouble CLK_TCK = 1000.0;
long Calc= 100;
//Prototypes
void Calibrar_Delay(int);
reloj startTimer(void);
void delay(unsignedlong);
double stopTimer(reloj);
usingnamespace std;
int main() {
//reloj r1, r2;
Calibrar_Delay(5); // Calibrate during 5 sec the function delay
return 0;
}
void Calibrar_Delay(int s=1){
reloj r;
double d;
r = startTimer();
cout << "##### r = startTimer() - Return number of ticks: " <<*r <<endl;
delay(1000); // Aprox 1s. Variar mucho inicialmente
d = stopTimer(r);
cout << "##### d = stopTimer(r) - Return the difference in the execution time: " <<d <<endl;
Calc= Calc/d;
cout << "##### Calc= Calc/d; - Calc: ; " <<Calc <<endl;
//Calc= int(double(Calc) / d);
//cout << "##### int(double(Calc) / d); - Calc: ; " <<Calc <<endl;
}
reloj startTimer() {
reloj c;
c= new clock_t; //Reservar memoria
*c= clock(); //Guardar numero de ticks
return c;
}
void delay(unsignedlong l) {
//Retardo de 1 ms
while(l--) {
for(long i=0; i < Calc; i++);
}
}
double stopTimer(reloj c){
clock_t diff;
diff= clock() - *c; //Diferencia entre Start y Stop
delete c; //Liberar memoria
return diff/CLK_TCK; //Pasar a segundos
}
I think the clock() function is not working properly this is the stdout of my execution:
##### r = startTimer() - Return number of ticks: 0
##### d = stopTimer(r) - Return the difference in the execution time: 0
##### Calc= Calc/d; - Calc: ; -2147483648
Here my questions:
1.- Why are "r" und "d" 0?
2.- What does the method delay() does?
I mean I know that makes a delay of 1ms but I don't understand the code to produce this delay.
Could you explain better what your code should do?
Perhaps you could find this page useful: http://en.cppreference.com/w/cpp/chrono/c/clock
I’ve more or less copied the following code from an example I found there: