delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2007/01/05/13:22:01

X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f
In-Reply-To: <OFF86BAC20.A8BCA0CB-ON8725725A.005C40FB-8725725A.005CCBAF@LocalDomain>
Subject: Re: Performance enhancement for gettimeofday()?
To: djgpp-workers AT delorie DOT com
X-Mailer: Lotus Notes Release 6.5.4 CCH5 September 12, 2005
Message-ID: <OFACB4C4B1.4472CEFA-ON8725725A.00631AB0-8725725A.00639428@seagate.com>
From: Gordon DOT Schumacher AT seagate DOT com
Date: Fri, 5 Jan 2007 11:07:39 -0700
X-MIMETrack: Serialize by Router on SV-GW1/Seagate Internet(Release 7.0.1 HF29|March 07, 2006) at
01/05/2007 10:09:51 AM
MIME-Version: 1.0
X-Proofpoint-FWRule: outbound2
X-Proofpoint-Virus-Version: vendor=fsecure engine=4.65.5446:2.3.11,1.2.37,4.0.164 definitions=2007-01-05_03:2007-01-03,2006-12-29,2007-01-05 signatures=0
Reply-To: djgpp-workers AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

Gordon Schumacher/Seagate wrote on 01/05/2007 09:53:34 AM:

# In fact, it would not even need to be done with an interrupt handler.
The
# first time the function is called, the int21/2C function could be called
# in order to determine the hundredths value that DOS is using.  Then the
# clock() function could be called to align those two values, and
thereafter
# the clock() function could be used instead.

Here's my proposal for a revised gettimeofday().  It initially uses the
DOS int21 functions (in fact it uses the exact current code) to retrieve
the current time and date, and also retrieves the current tick count.
Thereafter it calculates the new time and date based on the tick count
at the time it is called.

Accuracy/safety could perhaps be improved by putting in an extra check to
get the date and time from DOS when the elapsed tick count is greater than
a certain amount.

I've done some quick benchmarking; on my test system, a loop of 10,000
calls
to the current gettimeofday() routine takes 391 clock ticks; the new
routine
takes only five ticks!  (Quite a substantial improvement there...)

#include <libc/stubs.h>
#include <time.h>
#include <dpmi.h>

#define US_PER_CLOCK 10989

int gettimeofday(struct timeval *tv, struct timezone *tz)
{
   static clock_t startclock = 0;
   static struct timeval starttime;
   struct timeval tv_tmp;

   if (!tv)
     tv = &tv_tmp;

   if (startclock == 0)
   {
      __dpmi_regs r;
      struct tm tmblk;

      startclock = clock();

      r.h.ah = 0x2c;
      __dpmi_int(0x21, &r);
      starttime.tv_usec = r.h.dl * 10000;
      tmblk.tm_sec = r.h.dh;
      tmblk.tm_min = r.h.cl;
      tmblk.tm_hour = r.h.ch;

      r.h.ah = 0x2a;
      __dpmi_int(0x21, &r);

      tmblk.tm_mday = r.h.dl;
      tmblk.tm_mon = r.h.dh - 1;
      tmblk.tm_year = (r.x.cx & 0x7ff) - 1900;

      tmblk.tm_wday = tmblk.tm_yday = tmblk.tm_gmtoff = 0;
      tmblk.tm_zone = 0;
      tmblk.tm_isdst = -1;

      starttime.tv_sec = mktime(&tmblk);

      tv->tv_sec  = starttime.tv_sec;
      tv->tv_usec = starttime.tv_usec;
   }
   else
   {
      clock_t clockdiff = clock() - startclock;

      tv->tv_sec  = starttime.tv_sec  + (clockdiff / CLOCKS_PER_SEC);
      tv->tv_usec = starttime.tv_usec + ((clockdiff % CLOCKS_PER_SEC) *
US_PER_CLOCK);
   }

   if(tz)
   {
      struct tm *tmloc = localtime(&(tv->tv_sec));
      tz->tz_minuteswest = - tmloc->tm_gmtoff / 60;
      tz->tz_dsttime = tmloc->tm_isdst;
   }
   return 0;
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019