========================================================================== POTM folks - the following are several code remnants that can help you monitor your code ... one in straight C, one that is more suitable for Borland C users, a PERL snippet, a JAVA reference, and one for an VC++. All have been donated by POTM participants and may be incorporated in your entries as-is or with any modifications you wish ... NO GUARANTEES ... ========================================================================== #include /* for the TMS structure */ #define MAXTIME 600 /* 10 minutes worth of seconds */ #define CLK_TCK 100 /* Note: this may be different on YOUR box, you might find the value of HZ in YOUR param.h, but it's gotta be 100 when you ship your entry! Alternatively, use sysconf to deduce CLK_TCK - this approach should work on both systems! */ checktime() { int seconds; static struct tms TMS; times(&TMS); /* add the elapsed system and user times */ /* calculation comes out to the nearest second - rounded down */ seconds = (TMS.tms_stime + TMS.tms_utime)/CLK_TCK ; if (seconds >= MAXTIME) { /* PRINT OUT YOUR SOLUTION BEFORE YOU GO! */ exit(0); } } =============== CUT HERE .... IF YOU'RE USING BORLAND STUFF THE =============== FOLLOWING MAY BE USEFUL // // Includes the __BORLANDC__ define for compiling under Borland // // NOTE: Tabs set to 4 ... Thanks to Dave Peterson for the Borland stuff #define Revision 1.1 #include #include #include #ifdef __BORLANDC__ #include /* for the time_t structure */ #define MAXTIME 600 /* 10 minutes worth of seconds */ static time_t Started; void checktime(void) { time_t Current; time(&Current); if (difftime(Current, Started) > MAXTIME) { // display best moves exit(0); } } #else /* This has worked in the past, but please incorporate it into your program at your own risk ... your humble POTM-master is NOT much of a programmer ... */ #include /* for the TMS structure */ #define MAXTIME 600 /* 10 minutes worth of seconds */ #define CLK_TCK 100 /* Note: this may be different on YOUR box, you might find the value of HZ in YOUR param.h, but it's gotta be 100 when you ship your entry! Alternatively, use sysconf to deduce CLK_TCK - this approach should work on both systems! */ checktime() { int seconds; static struct tms TMS; times(&TMS); /* add the elapsed system and user times */ /* calculation comes out to the nearest second - rounded down */ seconds = (TMS.tms_stime + TMS.tms_utime)/CLK_TCK ; if (seconds >= MAXTIME) { // display best moves exit(0); } } #endif ############################ P E R L #################################### # The following donated by John Linderman, jpl@research.att.com .. THANKS! # Fred: assuming I stay within limits, what follows, up to the #####... line, # is a snippet that should work for Perl fans. I wrote it to be compatible # with perl 4, for the perl-impaired, and there are two interfaces # $used = &timeused(); # how much time have I used # $left = &timeleft(); # how much time is left # Feel free to make it available and by all means let me know # if anybody feels this is not a correct implementation. -- jpl $MAXTIME = 240; # Reverse with following line for shorter tests $MAXTIME = 600; # Number of seconds we are allowed sub times0 { local @t = times; local $i; for ($i = 0; $i < 4; $i++) { $t[$i] = 0 unless (defined($t[$i])); } @t; } @StartTimes = ×0(); sub timeused { local @t = ×0(); local $i; local $used; $used = 0; for ($i = 0; $i < 4; $i++) { $used += ($t[$i] - $StartTimes[$i]); } $used; } sub timeleft { $MAXTIME - &timeused(); } ######################### JAVA ######################################## /* This is a JAVA snippet that uses a Timer class to derive the elapsed (sys+user) processing time thus far. It makes use of a compiled c library which is referenced in the Timer class. This Timer class and library, referenced as below, will be supplied as part of the POTM environment. The code was supplied by Trevor Morris at trevor@lab.com and is supplied with his permission. */ /* Sample usage of the Timer class */ long maxTime = 600 * 1000; /* 10 minutes in milliseconds */ long prevTime = Timer.processMillis(); while (maxTime > Timer.processMillis()) { /* Processing goes here ... */ long newTime = Timer.processMillis(); System.out.println("Took " + ((double)(newTime - prevTime)/1000d) + " seconds so far." ); } System.out.println("Used " + Timer.processMillis() + " milliseconds total."); /* Copy this Timer class as given here ... it will compile on the POTM box and perform as expected ... try a test program and send it to fah@potm.lz.att.com if you wish. */ class Timer { static { System.loadLibrary("timer"); /* Nonstandard ... supplied by POTM */ } /** System usage time in milliseconds. * On UNIX, this is, TMS.tms_stime + TMS.tms_utime */ public static native long processMillis(); } ######################### VC++ ON NT ####################################### /* checktime() for Microsoft VC++ on Windows NT 3.5 or later */ /* Courtesy of Luc Kumps at kumps@attglobal.net */ #include #define MAXTIME 600 /* 10 minutes worth of seconds */ extern print_solution(); /* Define this in main program */ checktime() { int seconds; FILETIME ftCreation; FILETIME ftExit; FILETIME ftKernel; FILETIME ftUser; ULARGE_INTEGER liKernel; ULARGE_INTEGER liUser; GetProcessTimes(GetCurrentProcess(), &ftCreation, &ftExit, &ftKernel, &ftUser); /* Copy into 64-bit integer for easy arithmetic */ liKernel.LowPart = ftKernel.dwLowDateTime; liKernel.HighPart = ftKernel.dwHighDateTime; liUser.LowPart = ftUser.dwLowDateTime; liUser.HighPart = ftUser.dwHighDateTime; /* Convert 100ns into seconds */ liKernel.QuadPart /= 10000000; liUser.QuadPart /= 10000000; /* total = kernel + user, lazy rounding... */ seconds = liKernel.LowPart + liUser.LowPart + 1; /* Is it time to leave? */ if (seconds >= MAXTIME) { /* PRINT OUT YOUR SOLUTION BEFORE YOU GO! */ print_solution(); exit(0); } } ############################################################################