Functions to mesure the time - problem with "clock()" and method "void delay(unsigned long)"

I am coding to measure the time following an "already done program" from a book.
This is the on progress exercise:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <time.h>	// clock(), clock_t
#include <iostream>	// cout

typedef clock_t *reloj;
const double CLK_TCK = 1000.0;
long Calc= 100;

//Prototypes
void Calibrar_Delay(int);
reloj startTimer(void);
void delay(unsigned long);
double stopTimer(reloj);

using namespace 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(unsigned long 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.

Thanks.


Last edited on
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <ctime>
#include <iostream>
#include <limits>

void Calibrar_Delay();
void delay(unsigned long l);
void waitForEnter();

int main()
{
    Calibrar_Delay();
    waitForEnter();
    return 0;
}

void Calibrar_Delay()
{
    std::clock_t r = std::clock();
    std::cout << "##### r = startTimer() - Return number of ticks: " 
              << r << '\n';
    delay(10000);
    std::cout << "##### d = stopTimer(r) - Return the difference in the "
                 "execution time: " << (std::clock() - r) / CLOCKS_PER_SEC
              << '\n';
}

void delay(unsigned long l)
{
    // Perform some pointless calculation to take up some time
    // Copied from: http://en.cppreference.com/w/cpp/chrono/c/clock
    volatile double d = 0;
    for(unsigned long n=0; n<l; ++n) {
       for(unsigned long m=0; m<l; ++m) {
           d += d*n*m;
       }
    }
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Topic archived. No new replies allowed.