To: eliz AT is DOT elta DOT co DOT il Cc: djgpp-workers AT delorie DOT com References: Message-Id: <2.7.9.2472D.H44KS3@pauzner.dnttm.ru> From: "Leonid Pauzner" Date: Thu, 17 Oct 2002 16:16:51 +0400 (MSD) X-Mailer: dMail [Demos Mail for DOS v2.7.9] Subject: Re: libc' ctime.c optimizations MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Reply-To: djgpp-workers AT delorie DOT com >> I mean a caching within an hour directly inside mktime(), > As for `gettimeofday', what kind of applications calls it so massively as > to justify caching? Isn't it better to move this caching into > `gettimeofday' itself? Agree. Let we cache in gettimeofday(). gettimeofday() heavily used by select(). And many applications written by unix developers - perhaps gettimeofday more portable then usec timers. diff -u old/gettimeo.c ./gettimeo.c --- old/gettimeo.c Sat Mar 11 05:42:42 1995 +++ ./gettimeo.c Thu Oct 17 15:18:18 2002 @@ -2,6 +2,11 @@ #include #include #include +#include + +static int last_env_changed = 0; +static struct tm prev; +static int prev_sec; int gettimeofday(struct timeval *tv, struct timezone *tz) { @@ -32,7 +37,24 @@ tmblk.tm_zone = 0; tmblk.tm_isdst = -1; - tv->tv_sec = mktime(&tmblk); + /* cache expensive mktime() calls, until next hour */ + if (last_env_changed == __environ_changed && + prev.tm_hour == tmblk.tm_hour && + prev.tm_mday == tmblk.tm_mday && + prev.tm_mon == tmblk.tm_mon && + prev.tm_year == tmblk.tm_year) + { + tv->tv_sec = prev_sec + (tmblk.tm_sec - prev.tm_sec) + + (tmblk.tm_min - tmblk.tm_min)*60; + } + else + { + tv->tv_sec = mktime(&tmblk); + + prev_sec = tv->tv_sec; + prev = tmblk; + last_env_changed = __environ_changed; + } if(tz) {