Mail Archives: djgpp-workers/2002/10/17/08:31:50
>> 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 <libc/stubs.h>
#include <time.h>
#include <dpmi.h>
+#include <libc/environ.h>
+
+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)
{
- Raw text -