00001 /* 00002 timestart and timestop 00003 Sec. 1.7.2 00004 00005 Copyright 2003 by Marc J. Rochkind. All rights reserved. 00006 May be copied only for purposes and under conditions described 00007 on the Web page www.basepath.com/aup/copyright.htm. 00008 00009 The Example Files are provided "as is," without any warranty; 00010 without even the implied warranty of merchantability or fitness 00011 for a particular purpose. The author and his publisher are not 00012 responsible for any damages, direct or incidental, resulting 00013 from the use or non-use of these Example Files. 00014 00015 The Example Files may contain defects, and some contain deliberate 00016 coding mistakes that were included for educational reasons. 00017 You are responsible for determining if and how the Example Files 00018 are to be used. 00019 00020 */ 00021 #include "defs.h" 00022 00023 /*[timestart]*/ 00024 #include <sys/times.h> 00025 00026 static struct tms tbuf1; 00027 static clock_t real1; 00028 static long clock_ticks; 00029 00030 void timestart(void) 00031 { 00032 ec_neg1( real1 = times(&tbuf1) ) 00033 /* treat all -1 returns as errors */ 00034 ec_neg1( clock_ticks = sysconf(_SC_CLK_TCK) ) 00035 return; 00036 00037 EC_CLEANUP_BGN 00038 EC_FLUSH("timestart"); 00039 EC_CLEANUP_END 00040 } 00041 00042 void timestop(char *msg) 00043 { 00044 struct tms tbuf2; 00045 clock_t real2; 00046 00047 ec_neg1( real2 = times(&tbuf2) ) 00048 fprintf(stderr,"%s:\n\t\"Total (user/sys/real)\", %.2f, %.2f, %.2f\n" 00049 "\t\"Child (user/sys)\", %.2f, %.2f\n", msg, 00050 ((double)(tbuf2.tms_utime + tbuf2.tms_cutime) - 00051 (tbuf1.tms_utime + tbuf1.tms_cutime)) / clock_ticks, 00052 ((double)(tbuf2.tms_stime + tbuf2.tms_cstime) - 00053 (tbuf1.tms_stime + tbuf1.tms_cstime)) / clock_ticks, 00054 (double)(real2 - real1) / clock_ticks, 00055 (double)(tbuf2.tms_cutime - tbuf1.tms_cutime) / clock_ticks, 00056 (double)(tbuf2.tms_cstime - tbuf1.tms_cstime) / clock_ticks); 00057 return; 00058 00059 EC_CLEANUP_BGN 00060 EC_FLUSH("timestop"); 00061 EC_CLEANUP_END 00062 } 00063 /*[]*/