home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sys.hp
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!sdd.hp.com!hpscit.sc.hp.com!hpuerca.atl.hp.com!dtk
- From: dtk@atl.hp.com (Dara T. Khani)
- Subject: Re: Compiling a short C program with "cc - Aa"
- Message-ID: <BzsC1K.7E1@hpuerca.atl.hp.com>
- Sender: dtk@hpuerca.atl.hp.com ()
- Date: Thu, 24 Dec 1992 22:34:32 GMT
- References: <b2X=m-@engin.umich.edu>
- Organization: Hewlett-Packard Company, Atlanta GA
- Lines: 82
-
- In article <b2X=m-@engin.umich.edu>, oliveria@engin.umich.edu (Roque Donizete de Oliveira) writes:
- > Can someone tell me how to make this short program (just the function
- > "seconds" defined below) compilable with both "cc -O filename"
- > and "cc -O -Aa filename.c" ? Right now it compiles with
- > the former only. I'm using HPUX 8.07.
- > The reason I ask is that I'm modifying a C program to make it compilable
- > with both ANSI and non-ANSI C compilers, and on as many unix platforms
- > as possible. I'm using, in the big C code, ifdefs like:
- > #if defined(__STDC__) || defined(ANSI) || defined(NRANSI)
- >
- > The symbol "hpux" isn't defined if I use "cc -Aa".
- > If there is a better ANSI and non-ANSI C way of getting the elapsed cpu time
- > time of a program in as many unix platforms as possible, please let me know.
- > Thanks.
- > Roque
- > oliveria@engin.umich.edu
- >
- > Here is the same C code (just try it with cc and with and without the
- > -Aa option and you will see the compilation error when using -Aa).
- >
- >
- > #include <stdio.h> /* for printf */
- > /* Define a function to measure elapsed CPU time */
- > #ifdef hpux
- > #include <unistd.h> /* for sysconf */
- > #include <sys/times.h>
- > #else
- > #include <sys/time.h>
- > #include <sys/resource.h>
- > #endif
- > int
- > seconds()
- > {
- > #ifdef hpux
- > long ticks;
- > struct tms ruse;
- > ticks = sysconf(_SC_CLK_TCK);
- > times(&ruse);
- > return ((ruse.tms_utime + ruse.tms_stime)/ticks); /* user + system time used */
- > #else
- > struct rusage ruse;
- > getrusage(RUSAGE_SELF, &ruse);
- > return (ruse.ru_utime.tv_sec + ruse.ru_stime.tv_sec); /* user + system time used */
- > #endif
- > }
- >
- > main(argc, argv)
- > int argc;
- > char **argv;
- > {
- > int i, j, t0, t1;
- > t0 = seconds();
- > for (i=1;i<100000001;i++)
- > { j = 2*i + 5; }
- > t1 = seconds();
- > printf("t0= %d \n", t0);
- > printf("t1= %d \n", t1);
- > printf("j= %d \n", j);
- > printf("cpu usage: %d seconds \n", t1 - t0);
- > }
-
- --
- To gain access to names defined in POSIX and XOPEN you should add -D_HPUX_SOURCE
- to your cc when compiling with ANSI C (-Aa). So when you compile the above
- program with cc -Aa -D_HPUX_SOURCE file.c the linker says:
- Unsatisfied Symbol: getrusage.
-
- One step forward, one step back :(
- The routine getrusage is not provided in HP-UX. You can either write
- your own routine or use ifdef to isolate the call and use alternate calls.
- An HP-UX routine similar to getrusage is times(2).
-
- Hope this helps.
-
- - regards,
- Dara
-
- Dara T. Khani | Internet: dtk@atl.hp.com
- Hewlett-Packard Company | Tel: 404-988-3678
- 2000 South Park Place MS-S05 | Fax: 404-988-3682
- Atlanta, GA 30339 |
-
-