Re: gettimeofday being faster than getpid?

Rogier Wolff (R.E.Wolff@BitWizard.nl)
Mon, 8 Nov 1999 11:26:15 +0100 (MET)


Bret Indrelee wrote:
[Charset iso-8859-1 unsupported, filtering to ASCII...]
> Pavel Machek [mailto:pavel@bug.ucw.cz] wrote:
> > I did some tests, and when I replace getuid() or getpid() with
> > gettimeofday(), it actually gets faster.
> [ snip ]
> > void main(void)
> > {
> > struct timeval tv1, tv2, tv3;
> >
> > gettimeofday(&tv1, 0);
> > getuid(); <--- here
> > gettimeofday(&tv2, 0);
> >
> > printf("Time1: %d:%d\n", tv1.tv_sec, tv1.tv_usec);
> > printf("Time2: %d:%d\n", tv2.tv_sec, tv2.tv_usec);
> > }
>
> Not a very accurate way of measuring.
>
> Might I suggest:
>
> volatile int dummy1, dummy2;
> int loop;
>
> dummy2 = rand();
>
> gettimeofday(&tv1, 0);
> for (loop = 500000; loop >0 ; loop--) {
> dummy1 = dummy2 + 10;
> }
> gettimeofday(&tv2, 0);
>
> gettimeofday(&tv3, 0);
> for (loop = 500000; loop >0; loop--) {
> dummy1 = dummy2 + 10;
> statement_to_be_timed;
> }
> gettimeofday(&tv4, 0);

Oh, this may be a lot more accurate, but it measures different things.

It measures "hot-cache" performance.

gettimeofday should return microsecond accurate results. So,

> > gettimeofday(&tv1, 0);
> > getuid(); <--- here
> > gettimeofday(&tv2, 0);

should return results that are reasonably accurate. Replacing the
getuid() call with gettimeofday may even have cache effects, resulting
in the second and third call geting a hot cache.

Moreover, you can at least compare different things (and correcting
for the time gettimeofday takes) by putting different things in the
middle.

By comparing with

> > gettimeofday(&tv1, 0);
> > gettimeofday(&tv2, 0);

You can also eliminate the time that gettimeofday takes: you can count
on "gettimeofday" taking the "sample" at exactly the same point during
the dcall in both cases. So the difference should be exactly the time
it takes to make ONE gettimeofday call. Adding something in the
middle, the difference will give you the time that this thing in the
middle takes, and you can even make the measurement with "cold" cache.

So, the question remains: Why would gettimeofday be faster than
getuid()?

Answer: Hot cache possibly?

Pavel, is it still faster if you do

getuid ()

before the first call to getttimeofday?

Roger.

-- 
** R.E.Wolff@BitWizard.nl ** http://www.BitWizard.nl/ ** +31-15-2137555 **
*-- BitWizard writes Linux device drivers for any device you may have! --*
 "I didn't say it was your fault. I said I was going to blame it on you."

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/