Initial revision
[official-gcc.git] / gcc / f / runtime / libU77 / dtime_.c
blobe04ada1eca2fa78c99e0e58e440d93395082d838
1 /* Copyright (C) 1995, 1996 Free Software Foundation, Inc.
2 This file is part of GNU Fortran libU77 library.
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published
6 by the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 GNU Fortran is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with GNU Fortran; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 #if HAVE_UNISTD_H
23 # include <unistd.h>
24 #endif
25 #include <sys/times.h>
26 #if HAVE_GETRUSAGE
27 # include <sys/time.h>
28 # include <sys/resource.h>
29 #endif
30 #include "f2c.h"
32 /* For dtime, etime we store the clock tick parameter (clk_tck) the
33 first time either of them is invoked rather than each time. This
34 approach probably speeds up each invocation by avoiding a system
35 call each time, but means that the overhead of the first call is
36 different to all others. */
37 static long clk_tck = 0;
39 #ifdef KR_headers
40 doublereal G77_dtime_0 (tarray)
41 real tarray[2];
42 #else
43 doublereal G77_dtime_0 (real tarray[2])
44 #endif
46 time_t utime, stime;
47 static time_t old_utime = 0, old_stime = 0;
48 /* The getrusage version is only the default for convenience. */
49 #ifdef HAVE_GETRUSAGE
50 struct rusage rbuff;
52 if (getrusage (RUSAGE_SELF, &rbuff) != 0)
53 abort ();
54 utime = ((float) (rbuff.ru_utime).tv_sec +
55 (float) (rbuff.ru_utime).tv_usec/1000000.0);
56 tarray[0] = utime - (float) old_utime;
57 stime = ((float) (rbuff.ru_stime).tv_sec +
58 (float) (rbuff.ru_stime).tv_usec/1000000.0);
59 tarray[1] = stime - old_stime;
60 #else /* HAVE_GETRUSAGE */
61 struct tms buffer;
63 /* NeXTStep seems to define _SC_CLK_TCK but not to have sysconf;
64 fixme: does using _POSIX_VERSION help? */
65 # if defined _SC_CLK_TCK && defined _POSIX_VERSION
66 if (! clk_tck) clk_tck = sysconf(_SC_CLK_TCK);
67 # elif defined CLOCKS_PER_SECOND
68 if (! clk_tck) clk_tck = CLOCKS_PER_SECOND;
69 # elif defined CLK_TCK
70 if (! clk_tck) clk_tck = CLK_TCK;
71 # elif defined HAVE_GETRUSAGE
72 # else
73 #error Dont know clock tick length
74 # endif
75 if (times(&buffer) < 0) return -1.0;
76 utime = buffer.tms_utime; stime = buffer.tms_stime;
77 tarray[0] = ((float)(utime - old_utime)) / (float)clk_tck;
78 tarray[1] = ((float)(stime - old_stime)) / (float)clk_tck;
79 #endif /* HAVE_GETRUSAGE */
80 old_utime = utime; old_stime = stime;
81 return (tarray[0]+tarray[1]);