gnugo-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnugo-devel] gg_cputime()


From: Gunnar Farneback
Subject: [gnugo-devel] gg_cputime()
Date: Mon, 25 Mar 2002 17:56:29 +0100
User-agent: EMH/1.14.1 SEMI/1.14.3 (Ushinoya) FLIM/1.14.2 (Yagi-Nishiguchi) APEL/10.3 Emacs/20.7 (sparc-sun-solaris2.7) (with unibyte mode)

gg_cputime() is broken on unix and always has been. The code in
question looks like this:

#if HAVE_SYS_TIMES_H && HAVE_TIMES
    struct tms t;
    times(&t);
    return (t.tms_utime + t.tms_stime + t.tms_cutime + t.tms_cstime)
            / ((double) CLOCKS_PER_SEC);

The problem is that the struct tms fields are given in clock ticks,
which are unrelated to the CLOCKS_PER_SEC constant. That constant is
appropriate for converting the result of clock() to seconds.

We need to divide by some other value. According the man page for
times() on Solaris 8:

     All times are reported in clock ticks.  The  specific  value
     for  a  clock tick is defined by the variable CLK_TCK, found
     in the header <limits.h>.

However, my Linux man page says:

     The number of clock ticks per second can be obtained using
            sysconf(_SC_CLK_TCK);
     In POSIX-1996 the symbol CLK_TCK (defined in <time.h>)  is
     mentioned as obsolescent. It is obsolete now.

Looking closer at the Solaris limits.h file though, it turns out that
CLK_TCK is in fact a macro for the sysconf call, so there's no real
conflict here. Thus our code should be changed to

#if HAVE_SYS_TIMES_H && HAVE_TIMES && HAVE_UNISTD_H
    struct tms t;
    times(&t);
    return (t.tms_utime + t.tms_stime + t.tms_cutime + t.tms_cstime)
            / ((double) sysconf(_SC_CLK_TCK));

plus inclusion of the appropriate header (unistd.h).

The remaining question is whether there are any portability issues
with this solution. Does anyone know?

/Gunnar



reply via email to

[Prev in Thread] Current Thread [Next in Thread]