home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!zaphod.mps.ohio-state.edu!ub!acsu.buffalo.edu!sivap-s
- From: sivap-s@acsu.buffalo.edu (Suresh .S)
- Newsgroups: comp.unix.internals
- Subject: Re: Measuring the time of a function
- Message-ID: <BxxHGM.93I@acsu.buffalo.edu>
- Date: 18 Nov 92 20:12:21 GMT
- References: <356@gonzo.sfb313.uni-kiel.dbp.de>
- Sender: nntp@acsu.buffalo.edu
- Organization: UB
- Lines: 68
- Nntp-Posting-Host: thuban.cs.buffalo.edu
- X-Newsreader: TIN [version 1.1 PL6]
-
- Erik Cickovskis (erik@sfb313.uni-kiel.dbp.de) wrote:
- :
- : Is it possible to evaluate the time a function needs to execute in
- : C and UNIX?
- :
- : For my thesis I am writing C programms doing some mathematical
- : optimaziation stuff. I would like to know what time a function
- : needs, which executes the critical calculations.
- :
- : The problem is, now, that the functions in consideration need
- : about some milliseconds to execute, and I need to be quite
- : accurate.
- :
- : So what can I do to prevent kernel execution time to be measuered
- : with my function execution time. I thougt about it a lot and tend
- : to think it is impossible in a time sharing environment, but who
- : knows...
- :
- : Realise that I don't want the whole programm to be timed, just
- : one function in it.
-
- U can get the timing in the user space execution alone, avoinding the
- kernel execution ..
-
- You can use a utility called "prof" and gnu has it as "gprof"(correct
- me if I am wrong), to use this do the following. We have "cc" and that
- gives us this option
-
- (1) When you compile, do it with a "-p" option, and when you execute the
- program it will create a file called "mon.out".
-
- Now do "prof <name_of_executable>" and that will give u timings of all
- the functions in the program. I don't how accurate this is ..
-
- (2) Another option is to use the "times()" system call, here you can
- get seperate the execution in "user land" and "kernel land", of any
- particular function ..
-
- ------------------------CUT HERE-------------------------------------
- # include <stdio.h>
- # include <sys/types.h>
- # include <sys/times.h>
-
- struct tms buffer1,buffer2;
- long count1,count2;
- long times();
-
- main()
- {
- int i,j;
-
-
- count1 = times(&buffer1);
-
- /* Here you call the function */
-
- count2 = times(&buffer2);
-
- printf("User: %.2f\nSystem: %.2f \n", (float)(buffer2.tms_utime - buffer1.tms_utime)/60,(float)(buffer2.tms_stime - buffer1.tms_stime)/60);
- }
- ------------------------------------------------------------------------
-
- Hope that helps, let me know if you get any other solution also ..
-
- --
- Suresh Sivaprakasam
- Department of Computer Science, SUNY Buffalo, Amherst, NY - 14260-0001
- Internet :sivap-s@cs.Buffalo.EDU Bitnet : sivap-s@SUNYBCS.BITNET
-