On 8/19/05, guy keren <[EMAIL PROTECTED]> wrote:
>
> On Fri, 19 Aug 2005, Amos Shapira wrote:
>
> > On 8/19/05, guy keren <[EMAIL PROTECTED]> wrote:
> > > actually, it _looks_ like gettimeofday() sometimes takes less then a
> > > micro-second to execute. i tested it a few years ago on redhat 7.3, on a
> > > pentium 1.8MHz and an AMD athlon 2200+, and on both ofthem running
> > > gettimeofday in a tight loop very often yielded the same time (and
> > > gettimeofday returns the time in microsecond resolution).
[ snup ]
> the fact is, that i kept getting very close values - closer then the
That's the statement I was looking for (and didn't see) in your first
message - if they are identical then it could be that the call was simply
faster than system's the clock resolution. If they are "close" then it
means that the resolution is there.
I've just ran the following on my desktop (dual pentium-4 2.8GHz, 2.6.12.2,
but an IBM ThinkCentre which is known to have clock issues so it might be
suspicious) and the fastest I got between two calls to gettimeofday was 2
microseconds.
My conclusion from this excercise is that the resolution of the clock on this
system can be practically regarded at around 2 microsecond. When
you received "identical" results your program was just fast enough to run
two system calls before the resolution bumped the clock.
BTW - notice that there are no two identical values (no "0" line) - was your
code more efficient than this? ("g++-3.3 -O6" gave similar numbers, never
less than 3 microseconds).
#include <iostream>
#include <sys/time.h>
#include <time.h>
using namespace std;
int main(int argc, char *argv[])
{
for (int i = 0; i < 1000000; ++i)
{
struct timeval before, after, diff;
gettimeofday(&before, 0);
gettimeofday(&after, 0);
timersub(&after, &before, &diff);
if (diff.tv_sec) // cover my ass, never happened
cout << "Diff is more than a sec: " << diff.tv_sec << endl;
cout << diff.tv_usec << endl;
}
}
And here is the output:
$ ./a.out | sort -n | uniq -c | head
297 2
817177 3
173643 4
4090 5
1239 6
152 7
70 8
124 9
365 10
936 11
BTW - I tested this both with G++ 4.0.1 and 3.3.5, similar results.
Cheers,
--Amos
================================================================To unsubscribe,
send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]