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

  1. Newsgroups: comp.sys.hp
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!sdd.hp.com!hpscit.sc.hp.com!hpuerca.atl.hp.com!dtk
  3. From: dtk@atl.hp.com (Dara T. Khani)
  4. Subject: Re: Compiling a short C program with "cc - Aa"
  5. Message-ID: <BzsC1K.7E1@hpuerca.atl.hp.com>
  6. Sender: dtk@hpuerca.atl.hp.com ()
  7. Date: Thu, 24 Dec 1992 22:34:32 GMT
  8. References:  <b2X=m-@engin.umich.edu>
  9. Organization: Hewlett-Packard Company, Atlanta GA
  10. Lines: 82
  11.  
  12. In article <b2X=m-@engin.umich.edu>, oliveria@engin.umich.edu (Roque Donizete de Oliveira) writes:
  13. > Can someone tell me how to make this short program (just the function
  14. > "seconds" defined below) compilable with both "cc -O filename" 
  15. > and "cc -O -Aa filename.c" ? Right now it compiles with
  16. > the former only. I'm using HPUX 8.07.
  17. > The reason I ask is that I'm modifying a C program to make it compilable
  18. > with both ANSI and non-ANSI C compilers, and on as many unix platforms 
  19. > as possible. I'm using, in the big C code, ifdefs like:
  20. >            #if defined(__STDC__) || defined(ANSI) || defined(NRANSI)
  21. > The symbol "hpux" isn't defined if I use "cc -Aa".
  22. > If there is a better ANSI and non-ANSI C way of getting the elapsed cpu time
  23. > time of a program in as many unix platforms as possible, please let me know.
  24. > Thanks.
  25. >         Roque
  26. >         oliveria@engin.umich.edu
  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. > #include <stdio.h>  /* for printf */
  30. > /* Define a function to measure elapsed CPU time */
  31. > #ifdef hpux
  32. > #include <unistd.h> /* for sysconf */
  33. > #include <sys/times.h>
  34. > #else
  35. > #include <sys/time.h>
  36. > #include <sys/resource.h>
  37. > #endif
  38. > int
  39. > seconds()
  40. > {
  41. > #ifdef hpux
  42. >         long ticks;
  43. >         struct tms    ruse;
  44. >         ticks = sysconf(_SC_CLK_TCK);
  45. >     times(&ruse);  
  46. >     return ((ruse.tms_utime + ruse.tms_stime)/ticks);     /* user + system time used */
  47. > #else
  48. >     struct rusage ruse;
  49. >     getrusage(RUSAGE_SELF, &ruse);  
  50. >     return (ruse.ru_utime.tv_sec + ruse.ru_stime.tv_sec); /* user + system time used */
  51. > #endif
  52. > }
  53. > main(argc, argv)
  54. > int    argc;
  55. > char    **argv;
  56. > {  
  57. >    int i, j, t0, t1;
  58. >    t0 = seconds();
  59. >    for (i=1;i<100000001;i++) 
  60. >     {     j = 2*i + 5;     }
  61. >    t1 = seconds();   
  62. >    printf("t0= %d \n", t0);
  63. >    printf("t1= %d \n", t1);
  64. >    printf("j= %d \n", j);
  65. >    printf("cpu usage: %d seconds \n", t1 - t0);
  66. > }
  67.  
  68. -- 
  69. To gain access to names defined in POSIX and XOPEN you should add -D_HPUX_SOURCE
  70. to your cc when compiling with ANSI C (-Aa).  So when you compile the above
  71. program with cc -Aa -D_HPUX_SOURCE file.c  the linker says:
  72.  Unsatisfied Symbol:  getrusage.  
  73.  
  74. One step forward, one step back :(
  75. The routine getrusage is not provided in HP-UX.  You can either write 
  76. your own routine or use ifdef to isolate the call and use alternate calls.
  77. An HP-UX routine similar to getrusage is times(2).
  78.  
  79. Hope this helps.
  80.  
  81.                         - regards,
  82.                                   Dara
  83.  
  84.   Dara T. Khani                 | Internet: dtk@atl.hp.com 
  85.   Hewlett-Packard Company       | Tel:      404-988-3678 
  86.   2000 South Park Place  MS-S05 | Fax:      404-988-3682 
  87.   Atlanta, GA 30339             | 
  88.  
  89.