BRL-CAD
timer-nt.c
Go to the documentation of this file.
1 /* T I M E R - N T . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #include <string.h>
24 #include <time.h>
25 #include "bio.h"
26 
27 #include "vmath.h"
28 #include "raytrace.h"
29 
30 /* Standard System V stuff */
31 static clock_t start;
32 time_t time0;
33 
34 
35 void
37 {
38  start = clock();
39  time(&time0);
40 }
41 
42 
43 double
44 rt_get_timer(struct bu_vls *vp, double *elapsed)
45 {
46  time_t now;
47  double user_cpu_secs;
48  double sys_cpu_secs;
49  double elapsed_secs;
50  double percent;
51  clock_t finish;
52 
53  /* Real time. 1 second resolution. */
54  (void)time(&now);
55  elapsed_secs = difftime(now, time0);
56 
57  finish = clock();
58  sys_cpu_secs = (double)(finish - start);
59  sys_cpu_secs /= CLOCKS_PER_SEC;
60 
61  user_cpu_secs = sys_cpu_secs;
62 
63  if (user_cpu_secs < 0.00001) user_cpu_secs = 0.00001;
64  if (elapsed_secs < 0.00001) elapsed_secs = user_cpu_secs; /* It can't be any less! */
65 
66  if (elapsed) *elapsed = elapsed_secs;
67 
68  if (vp) {
69  percent = user_cpu_secs/elapsed_secs*100.0;
70  BU_CK_VLS(vp);
71  bu_vls_printf(vp,
72  "%g user + %g sys in %g elapsed secs (%g%%)",
73  user_cpu_secs, sys_cpu_secs, elapsed_secs, percent);
74  }
75  return user_cpu_secs;
76 }
77 
78 
79 double
80 rt_read_timer(char *str, int len)
81 {
82  struct bu_vls vls = BU_VLS_INIT_ZERO;
83  double cpu;
84  int todo;
85 
86  if (!str)
87  return rt_get_timer((struct bu_vls *)0, (double *)0);
88 
89  cpu = rt_get_timer(&vls, (double *)0);
90  todo = bu_vls_strlen(&vls);
91  if (todo > len)
92  todo = len;
93  bu_strlcpy(str, bu_vls_addr(&vls), todo);
94 
95  return cpu;
96 }
97 
98 
99 /*
100  * Local Variables:
101  * mode: C
102  * tab-width: 8
103  * indent-tabs-mode: t
104  * c-file-style: "stroustrup"
105  * End:
106  * ex: shiftwidth=4 tabstop=8
107  */
Header file for the BRL-CAD common definitions.
long time(time_t *)
double rt_read_timer(char *str, int len)
Definition: timer-nt.c:80
double rt_get_timer(struct bu_vls *vp, double *elapsed)
Definition: timer-nt.c:44
#define BU_CK_VLS(_vp)
Definition: vls.h:69
#define bu_strlcpy(dst, src, size)
Definition: str.h:60
time_t time0
Definition: timer-nt.c:32
size_t bu_vls_strlen(const struct bu_vls *vp)
Definition: vls.c:189
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define BU_VLS_INIT_ZERO
Definition: vls.h:84
Definition: vls.h:56
void rt_prep_timer(void)
Definition: timer-nt.c:36