C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-18-2008, 06:13 PM   #1
Registered User
 
Join Date: Jul 2008
Posts: 15
How to time how long a program takes to run?

Hi

I have written a C program that is supposed to run in under 2 seconds. Is there a function in C that lets me time how long it takes to run the program?

Thanks
advancedk is offline   Reply With Quote
Old 08-18-2008, 06:26 PM   #2
Wheres the lesbians?
 
mike_g's Avatar
 
Join Date: Oct 2006
Location: UK
Posts: 1,219
I often used clock() in time.h to get the time in millisecs. Only problem with that is that returns the number of clock cycles on linux so you have to divide it by CLOCKS_PER_SEC according to the manpage. Anyway its simple to use:
Code:
start_time = clock()
// do stuff
end_time = clock()
seconds_elapsed = (end_time - start_time) / 1000;
Alternatively theres gettimeofday(), which is more precise.
__________________
Senior highbrow doctor of authority.
mike_g is offline   Reply With Quote
Old 08-18-2008, 07:50 PM   #3
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,139
On Linux I'd be using time anyway.

Code:
[ ~]$ time ./myprog

real	0m3.855s
user	0m0.057s
sys	0m0.297s
Of course that method also counts how long your program takes to start.
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim
zacs7 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting a swf file in a windows application face_master Windows Programming 12 05-03-2009 11:29 AM
can't get this program to run correctly Amyaayaa C++ Programming 3 02-05-2008 04:16 PM
Merge and Heap..which is really faster silicon C++ Programming 2 05-10-2005 04:06 PM
Program in linux to do LOTS of math (takes long time to complete) Leeman_s Linux Programming 3 03-21-2004 06:02 PM
What's the code for a program to run in Task manager, under the processes tab newbie1 Windows Programming 5 04-17-2003 07:07 AM


All times are GMT -6. The time now is 10:29 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22