home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sys.hp
- Path: sparky!uunet!caen!oliveria
- From: oliveria@engin.umich.edu (Roque Donizete de Oliveira)
- Subject: Compiling a short C program with "cc - Aa"
- Message-ID: <b2X=m-@engin.umich.edu>
- Date: Thu, 24 Dec 92 15:59:15 EST
- Organization: University of Michigan Engineering, Ann Arbor
- Nntp-Posting-Host: minion.engin.umich.edu
- Lines: 59
-
- 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);
- }
-