File: timer.cc

package info (click to toggle)
gri 2.4.2-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 4,540 kB
  • ctags: 1,966
  • sloc: cpp: 32,542; lisp: 3,243; perl: 806; makefile: 548; sh: 253
file content (77 lines) | stat: -rw-r--r-- 1,447 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
#include <string>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <time.h>
#include <sys/times.h>


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()
{
    time_t          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