[Python-Dev] collecting timer resolution information...

Fred L. Drake, Jr. fdrake@acm.org
Tue, 26 Jun 2001 16:05:48 -0400 (EDT)


--FghjlKmKF6
Content-Type: text/plain; charset=us-ascii
Content-Description: message body and .signature
Content-Transfer-Encoding: 7bit


Fred L. Drake, Jr. writes:
 >   I'd like people to run the attached C program and send the output to

  OK, I've attached it this time.  Sorry!


  -Fred

-- 
Fred L. Drake, Jr.  <fdrake at acm.org>
PythonLabs at Digital Creations


--FghjlKmKF6
Content-Type: text/plain
Content-Description: timer check program
Content-Disposition: inline;
	filename="observation.c"
Content-Transfer-Encoding: 7bit

#include <stddef.h>
#include <sys/resource.h>
#include <sys/times.h>

/*
 * This should be defined the same way Python defines it:
 *
 * Define if gettimeofday() does not have second (timezone) argument.
 * This is the case on Motorola V4 (R40V4.2)
 */
/* #undef GETTIMEOFDAY_NO_TZ */


#ifdef GETTIMEOFDAY_NO_TZ
#define GETTIMEOFDAY(ptv) gettimeofday(ptv)
#else
#define GETTIMEOFDAY(ptv) gettimeofday((ptv), (struct timezone *)NULL)
#endif


static void
calibrate(int report)
{
    unsigned long timeofday_diff = 0;
    unsigned long rusage_diff = 0;
    unsigned long timeofday_calls = 0;
    unsigned long rusage_calls = 0;

    struct rusage ru1, ru2;
    struct timeval tv1, tv2;

    GETTIMEOFDAY(&tv1);
    while (1) {
        GETTIMEOFDAY(&tv2);
        ++timeofday_calls;
        if (tv1.tv_sec != tv2.tv_sec || tv1.tv_usec != tv2.tv_usec)
            break;
    }
    if (tv1.tv_sec == tv2.tv_sec)
        timeofday_diff = tv2.tv_usec - tv1.tv_usec;
    else
        timeofday_diff = (1000000 - tv1.tv_usec) + tv2.tv_usec;

    getrusage(RUSAGE_SELF, &ru1);
    while (1) {
        getrusage(RUSAGE_SELF, &ru2);
        ++rusage_calls;
        if (ru1.ru_utime.tv_sec != ru2.ru_utime.tv_sec) {
            rusage_diff = ((1000000 - ru1.ru_utime.tv_usec)
                           + ru2.ru_utime.tv_usec);
            break;
        }
        else if (ru1.ru_utime.tv_usec != ru2.ru_utime.tv_usec) {
            rusage_diff = ru2.ru_utime.tv_usec - ru1.ru_utime.tv_usec;
            break;
        }
        else if (ru1.ru_stime.tv_sec != ru2.ru_stime.tv_sec) {
            rusage_diff = ((1000000 - ru1.ru_stime.tv_usec)
                           + ru2.ru_stime.tv_usec);
            break;
        }
        else if (ru1.ru_stime.tv_usec != ru2.ru_stime.tv_usec) {
            rusage_diff = ru2.ru_stime.tv_usec - ru1.ru_stime.tv_usec;
            break;
        }
    }
    if (report)
        printf("timeofday: %lu (%lu calls), rusage: %lu (%lu calls)\n",
               timeofday_diff, timeofday_calls, rusage_diff, rusage_calls);
}


int
main(int argc, char *argv[])
{
    calibrate(0);
    calibrate(0);
    calibrate(1);
    return 0;
}

--FghjlKmKF6--