Zeugswetter Andreas SB  <[EMAIL PROTECTED]> writes:
>> HPUX has usleep, but the man page says
>> 
>> The usleep() function is included for its historical usage. The
>> setitimer() function is preferred over this function.

> I doubt that setitimer has microsecond precision on HPUX.

Well, if you insist on beating this into the ground:

$ cat timetest.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char** argv)
{
        int i;
        int delay;

        delay = atoi(argv[1]);

        for (i = 0; i < 1000; i++)
                usleep(delay);

        return 0;
}
$ gcc -O -Wall timetest.c
$ time ./a.out 1

real    0m20.02s
user    0m0.04s
sys     0m0.09s
$ time ./a.out 1000

real    0m20.04s
user    0m0.04s
sys     0m0.09s
$ time ./a.out 10000

real    0m20.01s
user    0m0.03s
sys     0m0.08s
$ time ./a.out 20000

real    0m30.03s
user    0m0.04s
sys     0m0.09s
$
$ cat timetest2.c
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>

typedef void (*pqsigfunc) (int);

pqsigfunc
pqsignal(int signo, pqsigfunc func)
{
        struct sigaction act,
                                oact;

        act.sa_handler = func;
        sigemptyset(&act.sa_mask);
        act.sa_flags = 0;
        if (signo != SIGALRM)
                act.sa_flags |= SA_RESTART;
        if (sigaction(signo, &act, &oact) < 0)
                return SIG_ERR;
        return oact.sa_handler;
}

void
catch_alarm(int sig)
{
}

int main(int argc, char** argv)
{
        int i;
        struct itimerval iv;
        int delay;

        delay = atoi(argv[1]);

        pqsignal(SIGALRM, catch_alarm);

        for (i = 0; i < 1000; i++)
        {
                iv.it_value.tv_sec = 0;
                iv.it_value.tv_usec = delay;
                iv.it_interval.tv_sec = 0;
                iv.it_interval.tv_usec = 0;
                setitimer(ITIMER_REAL, &iv, NULL);
                pause();
        }

        return 0;
}
$ gcc -O -Wall timetest2.c
$ time ./a.out 1

real    0m20.04s
user    0m0.01s
sys     0m0.05s
$ time ./a.out 1000

real    0m20.02s
user    0m0.01s
sys     0m0.06s
$ time ./a.out 10000

real    0m20.01s
user    0m0.01s
sys     0m0.05s
$ time ./a.out 20000

real    0m30.01s
user    0m0.01s
sys     0m0.06s
$

The usleep man page implies that usleep is actually implemented as a
setitimer call, which would explain the interchangeable results.  In
any case, neither one is useful for timing sub-clock-tick intervals;
in fact they're worse than select().

Anyone else want to try these examples on other platforms?

                        regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Reply via email to