By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 1,005 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Code for caluculating the execution time of C program

P: 1
Hi,
I require code for caluculating the execution time of C program .Can anybody help regarding this.

Regards,
Kameswari
Jun 21 '06 #1
Share this Question
Share on Google+
6 Replies


Banfa
Expert Mod 5K+
P: 8,916
Well that rather depends on what operating system you use. On a multitasking operating system like *nix and Windows (and several others) there is no generic way to measure the execution time from the program itself. You can measure the execution start time and the execution stop time however stop - start is not the execution time of the program. It is the execution time of the program plus all the time it was suspend while something else executed.

The only real way to get program execution time on a multi-tasking system is if the operating system provides a way to find out.
Jun 21 '06 #2

P: 1
Under Linux you can use the time command or for the detailed measure of time consumption of every procedure you can use the program gprof. See:
http://kuscsik.blogspot.com/2007/08/...using-gcc.html
Sep 5 '07 #3

gpraghuram
Expert 100+
P: 1,275
Hi,
I require code for caluculating the execution time of C program .Can anybody help regarding this.

Regards,
Kameswari
Hi ,
If u need to calculate execution time of the c code...u can use time function
The pseudo code will be like this
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. time_t t1,t2;
  4. time(&t1);
  5. //<your function goes here>
  6. time(&t2);
  7. diff_time(t2,t1)//check for function name
  8. }
  9.  
Thanks
Raghuram
Sep 5 '07 #4

P: 34
Hi Kamesari

This is sathish. What Raghu stated is fine.

Even me too had a requirement. I tried the program with difftime() and clock() and both were not giving the correct result.


I tried it under Sun Os. But whatever program raghu has given is correct.

Thanks & Regards
Sathish KUmar
Sep 5 '07 #5

P: 1
hi gpraghuram,
plz give me the code in C required for calculating the time for execution of any function. i m working in linux. the time( ) system call gives the time value in sec. but i need in milisec,or microsec...
help me out
Nov 27 '07 #6

gpraghuram
Expert 100+
P: 1,275
hi gpraghuram,
plz give me the code in C required for calculating the time for execution of any function. i m working in linux. the time( ) system call gives the time value in sec. but i need in milisec,or microsec...
help me out

Hi,
I dont know C-Library functions available which can give the execution time in Milli/micro seconds.

Raghuram
Nov 28 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.