www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/07/07/19:24:59

Sender: nate AT cartsys DOT com
Message-ID: <3783E08C.2BA33F77@cartsys.com>
Date: Wed, 07 Jul 1999 16:19:40 -0700
From: Nate Eldredge <nate AT cartsys DOT com>
X-Mailer: Mozilla 4.08 [en] (X11; I; Linux 2.2.10 i586)
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Re: Timing
References: <377CC7E6 DOT EB82B079 AT hotmail DOT com> <3780B0B3 DOT CE964094 AT hotmail DOT com>
Reply-To: djgpp AT delorie DOT com

Young Fan wrote:
> 
> Hi,
> 
> Thanks for your help. I've been trying to solve this problem for a whole
> week. There are very few comments in the DGJPP include files, so I was
> wondering if you tell me how to use both the uclock and the timestamp
> counter. This is what I found for uclock in sys/time.h:
> 
> #include <sys/types.h>
> typedef long long uclock_t;
> #define UCLOCKS_PER_SEC 1193180
> int gettimeofday(struct timeval *_tp, struct timezone *_tzp);
> unsigned long rawclock(void);
> int select(int _nfds, fd_set *_readfds, fd_set *_writefds, fd_set
> *_exceptfds, struct timeval *_timeout);
> int settimeofday(struct timeval *_tp, ...);
> uclock_t uclock(void);

The only one you need is `uclock'.  Thus:

uclock_t start, end;
start = uclock();
/* stuff to be timed */
end = uclock();
printf("It took %.15f seconds\n", ((double)(end - start)) /
UCLOCKS_PER_SEC);

The cast to double is important to get resolution better than 1 second.

> Would I still run into problems with Windows about this if I restarted
> in MS-DOS mode?

I can't imagine that you would.
 
> If the uclock doesn't work, how would I use the CPU's timestamp counter?

Here is a function to read the counter.

inline unsigned long long rdtsc(void)
{
  unsigned long long r;
  asm("rdtsc" : "=A" (r));
  return r;
}

You can use it like `uclock'.  Just replace `uclock_t' with `unsigned
long long', `uclock' with `rdtsc', and `UCLOCKS_PER_SEC' with your CPU's
clock speed in hertz.

HTH
-- 

Nate Eldredge
nate AT cartsys DOT com

- Raw text -


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