home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
misc
/
volume21
/
cloops
/
part01
/
secs.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-07-25
|
4KB
|
157 lines
/*
* This file is part of the Livermore Loops transliteration into C.
* Copyright (C) 1991 by Martin Fouts
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 1, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#define Float double
#ifdef BSD_TIME
#include <sys/types.h>
#include <sys/times.h>
#include <sys/time.h>
#define HZ 60
#else
#ifdef CONVEX_TIME
#include <sys/types.h>
#include <sys/times.h>
#include <sys/time.h>
#include <sys/resource.h>
#define HZ 60
#else
#include <sys/types.h> /* To avoid errors in param.h */
#include <sys/param.h> /* To obtain the local value for HZ */
#include <sys/times.h>
#endif
#endif
#ifdef ETA_BUG
float Hz;
static int hz = 100;
#endif
#define MICRO 1000000.0 /* number of microseconds in a second */
Float second(oldsec)
Float oldsec;
{
float mytime, cptime();
mytime = cptime();
return((Float)mytime-oldsec);
}
Float second_(oldsec)
Float *oldsec;
{
return second(*oldsec);
}
float cptime()
{
#ifdef CONVEX_TIME
struct rusage rusage;
float temp1, temp2;
getrusage(RUSAGE_SELF,&rusage);
temp1 = (float) rusage.ru_exutime.tv_sec;
temp2 = (float) rusage.ru_exutime.tv_usec / MICRO;
return(temp1+temp2);
#else
long times();
struct tms tms;
float t;
static oldt;
(void) times(&tms);
#ifdef ETA_BUG
Hz = hz;
t = (float) tms.tms_utime / Hz;
#else
t = (float)(tms.tms_utime /* + tms.tms_stime*/)/(float)HZ;
#endif
if (t < oldt) {
printf("cptime, new time (%f) < old!(%f)\n", t, oldt);
t = oldt;
}
oldt = t;
return(t);
#endif
}
float secs() /* returns the number of seconds which have
* expired since secs() was last called, to
* the highest granularity available on the
* system:
* SVR2: (vax) 1/60 second
* 4.2 BSD: (vax) 1/100 second
* Cray 2 1/250 millionth second
*/
{
#ifdef BSD_TIME /* There are really two versions, one for
* BSD and one for SVR2. If you have a
* different way of getting time, add
* another.
*/
static struct timeval tpbase;/* time at which secs() first called */
struct timeval tp; /* time at current call */
struct timezone tzp; /* ignored */
float temp, temp1, temp2; /* To simplify the code */
(void)gettimeofday(&tp,&tzp); /* find out what time it is */
if (tpbase.tv_sec == 0) { /* first time, set the base */
tpbase.tv_sec = tp.tv_sec;
tpbase.tv_usec = tp.tv_usec;
}
/* calculate the time since last called, as floating seconds */
temp1 = (float) (tp.tv_sec - tpbase.tv_sec);
temp2 = (float) (tp.tv_usec - tpbase.tv_usec) / MICRO ;
temp = temp1 + temp2;
tpbase.tv_sec = tp.tv_sec;
tpbase.tv_usec = tp.tv_usec;
return(temp);
#else
#ifdef CRAY_TIME
static long rtticks;
long rtclock();
long rtnow = rtclock();
float rettime = (float)(rtnow-rtticks) / (float) HZ;
rtticks = rtnow;
return(rettime);
#else
struct tms tms; /* dummy (as in not used) */
static long nowtim = 0; /* time at which secs() first called */
long thistim; /* current time (in tics) */
long times(); /* returns ticks (ticks/HZ = seconds) */
float rettime; /* Time to return */
thistim = times(&tms); /* get the time */
if (nowtim == 0) nowtim = thistim;
#ifdef ETA_BUG
Hz = hz;
rettime = (float)(thistim-nowtim)/Hz;
#else
rettime = (float)(thistim-nowtim)/(float)HZ;
#endif ETA_BUG
nowtim = thistim; /* first call */
return(rettime); /* return, converted to secs */
#endif CRAY_TIME
#endif BSD_TIME
}