home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / unix / internal / 1965 < prev    next >
Encoding:
Text File  |  1992-11-18  |  2.7 KB  |  82 lines

  1. Path: sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!zaphod.mps.ohio-state.edu!ub!acsu.buffalo.edu!sivap-s
  2. From: sivap-s@acsu.buffalo.edu (Suresh .S)
  3. Newsgroups: comp.unix.internals
  4. Subject: Re: Measuring the time of a function
  5. Message-ID: <BxxHGM.93I@acsu.buffalo.edu>
  6. Date: 18 Nov 92 20:12:21 GMT
  7. References: <356@gonzo.sfb313.uni-kiel.dbp.de>
  8. Sender: nntp@acsu.buffalo.edu
  9. Organization: UB
  10. Lines: 68
  11. Nntp-Posting-Host: thuban.cs.buffalo.edu
  12. X-Newsreader: TIN [version 1.1 PL6]
  13.  
  14. Erik Cickovskis (erik@sfb313.uni-kiel.dbp.de) wrote:
  15. : Is it possible to evaluate the time a function needs to execute in
  16. : C and UNIX?
  17. : For my thesis I am writing C programms doing some mathematical
  18. : optimaziation stuff. I would like to know what time a function
  19. : needs, which executes the critical calculations.
  20. : The problem is, now, that the functions in consideration need
  21. : about some milliseconds to execute, and I need to be quite
  22. : accurate.
  23. : So what can I do to prevent kernel execution time to be measuered
  24. : with my function execution time. I thougt about it a lot and tend
  25. : to think it is impossible in a time sharing environment, but who
  26. : knows...
  27. : Realise that I don't want the whole programm to be timed, just
  28. : one function in it.
  29.  
  30. U can get the timing in the user  space execution alone, avoinding the
  31. kernel execution ..
  32.  
  33. You can use a utility called "prof" and gnu  has it as "gprof"(correct
  34. me if I am wrong), to use this do the following. We have "cc" and that
  35. gives us this option
  36.  
  37. (1) When you compile, do it with a  "-p" option, and  when you execute the
  38. program it will create a file called "mon.out".
  39.  
  40. Now do "prof <name_of_executable>" and that will give u timings of all
  41. the functions in the program. I don't how accurate this is ..
  42.  
  43. (2) Another option is to use  the "times()" system  call, here you can
  44. get  seperate  the execution in "user  land" and "kernel land", of any
  45. particular function ..
  46.  
  47. ------------------------CUT HERE-------------------------------------
  48. # include  <stdio.h>
  49. # include <sys/types.h>
  50. # include <sys/times.h>
  51.  
  52.  struct tms buffer1,buffer2;
  53.  long count1,count2;
  54.  long times();
  55.  
  56. main()
  57.    int i,j;
  58.  
  59.   
  60.    count1 = times(&buffer1);
  61.  
  62.  /* Here you call the function */
  63.  
  64.    count2 = times(&buffer2);
  65.  
  66. printf("User: %.2f\nSystem: %.2f \n", (float)(buffer2.tms_utime -  buffer1.tms_utime)/60,(float)(buffer2.tms_stime -  buffer1.tms_stime)/60);
  67. }
  68. ------------------------------------------------------------------------
  69.  
  70. Hope that helps, let me know if you get any other solution also ..
  71.  
  72. -- 
  73. Suresh Sivaprakasam                    
  74. Department of Computer Science,    SUNY Buffalo,    Amherst,   NY - 14260-0001
  75. Internet :sivap-s@cs.Buffalo.EDU               Bitnet : sivap-s@SUNYBCS.BITNET
  76.