Re: a faster way to gettimeofday?

Chris Friesen (cfriesen@nortelnetworks.com)
Wed, 06 Mar 2002 11:16:08 -0500


Ben Greear wrote:
>
> I have a program that I very often need to calculate the current
> time, with milisecond accuracy. I've been using gettimeofday(),
> but gprof shows it's taking a significant (10% or so) amount of
> time. Is there a faster (and perhaps less portable?) way to get
> the time information on x86? My program runs as root, so should
> have any permissions it needs to use some backdoor hack if that
> helps!

#include <asm/msr.h>

/* get this value from the "cpu MHz" line of /proc/cpuinfo */
#define CLOCKSPEED xxxxxxxx

int main()
{
unsigned int lowbegin, lowend, highbegin, highend;
unsigned long long diff;
double elapsed;

rdtsc(lowbegin,highbegin);

//do stuff

rdtsc(lowend,highend);

if (lowend < lowbegin)
highend--;

diff = (((unsigned long long) highend - highbegin) << 32) + (lowend -
lowbegin);

elapsed = (double) diff / CLOCKSPEED;

/* elapsed now has time in microseconds, do whatever you wantwith it */

return 0;
}

-- 
Chris Friesen                    | MailStop: 043/33/F10  
Nortel Networks                  | work: (613) 765-0557
3500 Carling Avenue              | fax:  (613) 765-2986
Nepean, ON K2H 8E9 Canada        | email: cfriesen@nortelnetworks.com
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/