1159b3361Sopenharmony_ci/* 2159b3361Sopenharmony_ci * Lame time routines source file 3159b3361Sopenharmony_ci * 4159b3361Sopenharmony_ci * Copyright (c) 2000 Mark Taylor 5159b3361Sopenharmony_ci * 6159b3361Sopenharmony_ci * This library is free software; you can redistribute it and/or 7159b3361Sopenharmony_ci * modify it under the terms of the GNU Library General Public 8159b3361Sopenharmony_ci * License as published by the Free Software Foundation; either 9159b3361Sopenharmony_ci * version 2 of the License, or (at your option) any later version. 10159b3361Sopenharmony_ci * 11159b3361Sopenharmony_ci * This library is distributed in the hope that it will be useful, 12159b3361Sopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 13159b3361Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14159b3361Sopenharmony_ci * Library General Public License for more details. 15159b3361Sopenharmony_ci * 16159b3361Sopenharmony_ci * You should have received a copy of the GNU Library General Public 17159b3361Sopenharmony_ci * License along with this library; if not, write to the 18159b3361Sopenharmony_ci * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 19159b3361Sopenharmony_ci * Boston, MA 02111-1307, USA. 20159b3361Sopenharmony_ci */ 21159b3361Sopenharmony_ci 22159b3361Sopenharmony_ci/* $Id$ */ 23159b3361Sopenharmony_ci 24159b3361Sopenharmony_ci/* 25159b3361Sopenharmony_ci * name: GetCPUTime ( void ) 26159b3361Sopenharmony_ci * 27159b3361Sopenharmony_ci * description: returns CPU time used by the process 28159b3361Sopenharmony_ci * input: none 29159b3361Sopenharmony_ci * output: time in seconds 30159b3361Sopenharmony_ci * known bugs: may not work in SMP and RPC 31159b3361Sopenharmony_ci * conforming: ANSI C 32159b3361Sopenharmony_ci * 33159b3361Sopenharmony_ci * There is some old difficult to read code at the end of this file. 34159b3361Sopenharmony_ci * Can someone integrate this into this function (if useful)? 35159b3361Sopenharmony_ci */ 36159b3361Sopenharmony_ci 37159b3361Sopenharmony_ci#ifdef HAVE_CONFIG_H 38159b3361Sopenharmony_ci# include <config.h> 39159b3361Sopenharmony_ci#endif 40159b3361Sopenharmony_ci 41159b3361Sopenharmony_ci#include <assert.h> 42159b3361Sopenharmony_ci#include <stdio.h> 43159b3361Sopenharmony_ci#include <time.h> 44159b3361Sopenharmony_ci 45159b3361Sopenharmony_ci#ifdef WITH_DMALLOC 46159b3361Sopenharmony_ci#include <dmalloc.h> 47159b3361Sopenharmony_ci#endif 48159b3361Sopenharmony_ci 49159b3361Sopenharmony_ci#include "lametime.h" 50159b3361Sopenharmony_ci 51159b3361Sopenharmony_ci#if !defined(CLOCKS_PER_SEC) 52159b3361Sopenharmony_ci# warning Your system does not define CLOCKS_PER_SEC, guessing one... 53159b3361Sopenharmony_ci# define CLOCKS_PER_SEC 1000000 54159b3361Sopenharmony_ci#endif 55159b3361Sopenharmony_ci 56159b3361Sopenharmony_ci 57159b3361Sopenharmony_cidouble 58159b3361Sopenharmony_ciGetCPUTime(void) 59159b3361Sopenharmony_ci{ 60159b3361Sopenharmony_ci clock_t t; 61159b3361Sopenharmony_ci 62159b3361Sopenharmony_ci#if defined(_MSC_VER) || defined(__BORLANDC__) 63159b3361Sopenharmony_ci t = clock(); 64159b3361Sopenharmony_ci#else 65159b3361Sopenharmony_ci t = clock(); 66159b3361Sopenharmony_ci#endif 67159b3361Sopenharmony_ci return t / (double) CLOCKS_PER_SEC; 68159b3361Sopenharmony_ci} 69159b3361Sopenharmony_ci 70159b3361Sopenharmony_ci 71159b3361Sopenharmony_ci/* 72159b3361Sopenharmony_ci * name: GetRealTime ( void ) 73159b3361Sopenharmony_ci * 74159b3361Sopenharmony_ci * description: returns real (human) time elapsed relative to a fixed time (mostly 1970-01-01 00:00:00) 75159b3361Sopenharmony_ci * input: none 76159b3361Sopenharmony_ci * output: time in seconds 77159b3361Sopenharmony_ci * known bugs: bad precision with time() 78159b3361Sopenharmony_ci */ 79159b3361Sopenharmony_ci 80159b3361Sopenharmony_ci#if defined(__unix__) || defined(SVR4) || defined(BSD) 81159b3361Sopenharmony_ci 82159b3361Sopenharmony_ci# include <sys/time.h> 83159b3361Sopenharmony_ci# include <unistd.h> 84159b3361Sopenharmony_ci 85159b3361Sopenharmony_cidouble 86159b3361Sopenharmony_ciGetRealTime(void) 87159b3361Sopenharmony_ci{ /* conforming: SVr4, BSD 4.3 */ 88159b3361Sopenharmony_ci struct timeval t; 89159b3361Sopenharmony_ci 90159b3361Sopenharmony_ci if (0 != gettimeofday(&t, NULL)) 91159b3361Sopenharmony_ci assert(0); 92159b3361Sopenharmony_ci return t.tv_sec + 1.e-6 * t.tv_usec; 93159b3361Sopenharmony_ci} 94159b3361Sopenharmony_ci 95159b3361Sopenharmony_ci#elif defined(WIN16) || defined(WIN32) 96159b3361Sopenharmony_ci 97159b3361Sopenharmony_ci# include <stdio.h> 98159b3361Sopenharmony_ci# include <sys/types.h> 99159b3361Sopenharmony_ci# include <sys/timeb.h> 100159b3361Sopenharmony_ci 101159b3361Sopenharmony_cidouble 102159b3361Sopenharmony_ciGetRealTime(void) 103159b3361Sopenharmony_ci{ /* conforming: Win 95, Win NT */ 104159b3361Sopenharmony_ci struct timeb t; 105159b3361Sopenharmony_ci 106159b3361Sopenharmony_ci ftime(&t); 107159b3361Sopenharmony_ci return t.time + 1.e-3 * t.millitm; 108159b3361Sopenharmony_ci} 109159b3361Sopenharmony_ci 110159b3361Sopenharmony_ci#else 111159b3361Sopenharmony_ci 112159b3361Sopenharmony_cidouble 113159b3361Sopenharmony_ciGetRealTime(void) 114159b3361Sopenharmony_ci{ /* conforming: SVr4, SVID, POSIX, X/OPEN, BSD 4.3 */ /* BUT NOT GUARANTEED BY ANSI */ 115159b3361Sopenharmony_ci time_t t; 116159b3361Sopenharmony_ci 117159b3361Sopenharmony_ci t = time(NULL); 118159b3361Sopenharmony_ci return (double) t; 119159b3361Sopenharmony_ci} 120159b3361Sopenharmony_ci 121159b3361Sopenharmony_ci#endif 122159b3361Sopenharmony_ci 123159b3361Sopenharmony_ci 124159b3361Sopenharmony_ci#if defined(_WIN32) || defined(__CYGWIN__) 125159b3361Sopenharmony_ci# include <io.h> 126159b3361Sopenharmony_ci# include <fcntl.h> 127159b3361Sopenharmony_ci#else 128159b3361Sopenharmony_ci# include <unistd.h> 129159b3361Sopenharmony_ci#endif 130159b3361Sopenharmony_ci 131159b3361Sopenharmony_ciint 132159b3361Sopenharmony_cilame_set_stream_binary_mode(FILE * const fp) 133159b3361Sopenharmony_ci{ 134159b3361Sopenharmony_ci#if defined __EMX__ 135159b3361Sopenharmony_ci _fsetmode(fp, "b"); 136159b3361Sopenharmony_ci#elif defined __BORLANDC__ 137159b3361Sopenharmony_ci setmode(_fileno(fp), O_BINARY); 138159b3361Sopenharmony_ci#elif defined __CYGWIN__ 139159b3361Sopenharmony_ci setmode(fileno(fp), _O_BINARY); 140159b3361Sopenharmony_ci#elif defined _WIN32 141159b3361Sopenharmony_ci _setmode(_fileno(fp), _O_BINARY); 142159b3361Sopenharmony_ci#else 143159b3361Sopenharmony_ci (void) fp; /* doing nothing here, silencing the compiler only. */ 144159b3361Sopenharmony_ci#endif 145159b3361Sopenharmony_ci return 0; 146159b3361Sopenharmony_ci} 147159b3361Sopenharmony_ci 148159b3361Sopenharmony_ci 149159b3361Sopenharmony_ci/* End of lametime.c */ 150