File: timer.cc

package info (click to toggle)
gri 2.8.7-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,472 kB
  • ctags: 2,092
  • sloc: cpp: 33,255; lisp: 3,980; perl: 1,037; makefile: 668; sh: 301
file content (80 lines) | stat: -rw-r--r-- 1,442 bytes parent folder | download
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
#include <string>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <time.h>
#if defined(HAVE_POSIX_TIMES)
#include <sys/times.h>
#endif
#include "gr.hh"


void gr_textput(const char *s);
#include "macro.hh"
#include "GriTimer.hh"
bool show_stopwatchCmd(void);

bool
show_stopwatchCmd(void)
{
	char msg[100];
	static bool first = true;
#if defined(HAVE_POSIX_TIMES)
	struct tms buffer;
	static clock_t last, now;
#else
	static time_t last;
#endif
	if (first == true) {
#if defined(HAVE_POSIX_TIMES)
		last = times(&buffer);
#else
		time(&last);
#endif
		first = false;
		sprintf(msg, "Elapsed time = 0 s\n");
	} else {
#if defined(HAVE_POSIX_TIMES)
		now = times(&buffer);
		sprintf(msg, "Elapsed time = %.3f s\n", float(now - last) / sysconf(_SC_CLK_TCK));
#else
		double elapsed;
		static time_t now;
		time(&now);
		elapsed = now - last;
		sprintf(msg, "Elapsed time = %.0f s\n", elapsed);
#endif
	}
	ShowStr(msg);
	return true;
}

GriTimer::GriTimer()
{
#if defined(HAVE_POSIX_TIMES)
	struct tms buffer;
	start = times(&buffer);
#else
	time(&start);
#endif
}
char* GriTimer::now_ascii()
{
	SECOND_TYPE sec;
	time(&sec);
	return(asctime(localtime(&sec)));
}
double GriTimer::elapsed_time()
{
#if defined(HAVE_POSIX_TIMES)
	struct tms buffer;
	static clock_t now = times(&buffer);
	return double(now - start) / sysconf(_SC_CLK_TCK);
#else
	static time_t now;
	time(&now);
	return double(now - start);
#endif
}

// Main