home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / sys / hp / 14364 < prev    next >
Encoding:
Text File  |  1992-12-24  |  2.1 KB  |  70 lines

  1. Newsgroups: comp.sys.hp
  2. Path: sparky!uunet!caen!oliveria
  3. From: oliveria@engin.umich.edu (Roque Donizete de Oliveira)
  4. Subject: Compiling a short C program with "cc - Aa"
  5. Message-ID: <b2X=m-@engin.umich.edu>
  6. Date: Thu, 24 Dec 92 15:59:15 EST
  7. Organization: University of Michigan Engineering, Ann Arbor
  8. Nntp-Posting-Host: minion.engin.umich.edu
  9. Lines: 59
  10.  
  11. Can someone tell me how to make this short program (just the function
  12. "seconds" defined below) compilable with both "cc -O filename" 
  13. and "cc -O -Aa filename.c" ? Right now it compiles with
  14. the former only. I'm using HPUX 8.07.
  15. The reason I ask is that I'm modifying a C program to make it compilable
  16. with both ANSI and non-ANSI C compilers, and on as many unix platforms 
  17. as possible. I'm using, in the big C code, ifdefs like:
  18.            #if defined(__STDC__) || defined(ANSI) || defined(NRANSI)
  19.  
  20. The symbol "hpux" isn't defined if I use "cc -Aa".
  21. If there is a better ANSI and non-ANSI C way of getting the elapsed cpu time
  22. time of a program in as many unix platforms as possible, please let me know.
  23. Thanks.
  24.         Roque
  25.         oliveria@engin.umich.edu
  26.  
  27. Here is the same C code (just try it with cc and with and without the
  28. -Aa option and you will see the compilation error when using -Aa).
  29.  
  30.  
  31. #include <stdio.h>  /* for printf */
  32. /* Define a function to measure elapsed CPU time */
  33. #ifdef hpux
  34. #include <unistd.h> /* for sysconf */
  35. #include <sys/times.h>
  36. #else
  37. #include <sys/time.h>
  38. #include <sys/resource.h>
  39. #endif
  40. int
  41. seconds()
  42. {
  43. #ifdef hpux
  44.         long ticks;
  45.         struct tms    ruse;
  46.         ticks = sysconf(_SC_CLK_TCK);
  47.     times(&ruse);  
  48.     return ((ruse.tms_utime + ruse.tms_stime)/ticks);     /* user + system time used */
  49. #else
  50.     struct rusage ruse;
  51.     getrusage(RUSAGE_SELF, &ruse);  
  52.     return (ruse.ru_utime.tv_sec + ruse.ru_stime.tv_sec); /* user + system time used */
  53. #endif
  54. }
  55.  
  56. main(argc, argv)
  57. int    argc;
  58. char    **argv;
  59. {  
  60.    int i, j, t0, t1;
  61.    t0 = seconds();
  62.    for (i=1;i<100000001;i++) 
  63.     {     j = 2*i + 5;     }
  64.    t1 = seconds();   
  65.    printf("t0= %d \n", t0);
  66.    printf("t1= %d \n", t1);
  67.    printf("j= %d \n", j);
  68.    printf("cpu usage: %d seconds \n", t1 - t0);
  69. }
  70.