File: ctimer.h

package info (click to toggle)
libfann 2.1.0~beta%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 14,648 kB
  • ctags: 924
  • sloc: sh: 8,753; ansic: 5,994; cpp: 2,351; makefile: 507; perl: 243; python: 131; sed: 7
file content (92 lines) | stat: -rw-r--r-- 1,859 bytes parent folder | download | duplicates (4)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <time.h>

clock_t startTime = 0;
clock_t middleEnd = 0;
clock_t middleBegin = 0;
clock_t endTime = 0;
clock_t curtime = 0;
double timePerIteration = 0;

clock_t getBeginTime()
{
	clock_t beginTime;
	clock_t lastTime = clock();
	while((beginTime = clock()) == lastTime);
	return beginTime;
}
	
clock_t getEndTime()
{
	unsigned int i = 0;
	clock_t beginTime = clock();
	clock_t endTime = beginTime;
	clock_t newEnd;
	while((endTime = clock()) == beginTime) ++i;
	
	newEnd = endTime - (clock_t)(timePerIteration * (double)(i));
	return (newEnd > beginTime) ? newEnd : beginTime;
}


void start_timer()
{
	middleEnd = 0;
	middleBegin = 0;
	endTime = 0;
	curtime = 0;
	
	startTime = getBeginTime();
}

void middle_time()
{
	middleEnd = getEndTime();
	middleBegin = getBeginTime();
}

double time_elapsed()
{
	return (double)(clock() - startTime) / (double)(CLOCKS_PER_SEC);
}

void stop_timer()
{
	endTime = getEndTime();
	
	if(middleEnd){
		curtime = (endTime - middleEnd) - (middleBegin - startTime);
	}else{
		curtime = endTime - startTime;
	}
}

double getSecs()
{
	return (double)(curtime) / (double)(CLOCKS_PER_SEC);
}

double getNanoPerN(unsigned int N)
{
	return (getSecs() / (double)(N)) * (double)(1.0e9);
}

void calibrate_timer()
{
	/* Calculating timePerIteration this will take arround a second. */
	clock_t real_begin;
	clock_t real_end;
	unsigned int i = 0;
	clock_t lastTime = getBeginTime();
	real_begin = lastTime;
	real_end = real_begin + CLOCKS_PER_SEC;
	/* Looks a lot like the loop in getEndTime */
	while((lastTime = clock()) < real_end)	++i;
	
	timePerIteration = (double)(i) / (double)(lastTime - real_begin);
}

void printStatus()
{
	printf("startTime = %lu, middleEnd = %lu, middleBegin = %lu, endTime = %lu, curtime = %lu, timePerIteration = %f\n", 
		startTime, middleEnd, middleBegin, endTime, curtime, timePerIteration);
}