C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 7th, 2003, 02:14 AM
SonicWave SonicWave is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Posts: 12 SonicWave User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question How to calculate duration of a process in seconds

Good day !

I have a problem here. I got a function call normalSearch to perform a linear search on an array. I wish to time the duration taken for the searching process. So I coded my program this way:

clock_t timeStart, timeEnd;
timeStart = clock();
searchResult = normalSearch(searchString, testArray);
timeEnd = clock();
timeUsed = (double)(timeEnd-timeStart)/CLK_TCK;

I thought it should return the timeUsed in the format of x.xx.
But weirdly enough, what I got is that, the timeStart is equal timeEnd ?

How should I modify the function so that it can calculate the time correctly ?

Thanks a lot !

SonicWave

Reply With Quote
  #2  
Old January 8th, 2003, 01:41 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 10 h 16 m 27 sec
Reputation Power: 4080
Maybe your normalSearch() function is working too fast, which is why timeStart = timeEnd. See if gettimeofday() will help?

Reply With Quote
  #3  
Old January 8th, 2003, 09:51 PM
TechNoFear TechNoFear is offline
Offensive Member
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2002
Location: in the perfect world
Posts: 618 TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 13 h 50 m
Reputation Power: 26
Timers are very inaccurate.

Under window OS timer messages are the lowest prority (except some paint msg's). They keep getting dropped back to the bottom of the msg que as more inportant msg's arrive.

CLK_TCK has been obsolete since win95, use CLOCKS_PER_SEC (CLK_TCK is #defined as CLOCKS_PER_SEC anyway).

It is in milliseconds so they both are 1000.
__________________
The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

Frank Zappa

Reply With Quote
  #4  
Old January 9th, 2003, 02:00 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 10 h 16 m 27 sec
Reputation Power: 4080
The WM_TIMER message may be a low priority, but getting the time from the real time clock is definitely much more accurate. This is because the clock interrupt runs on a very high priority (IIRC, only the Non-Maskable Interrupt (NMI) runs higher). The trouble with using the real time clock is that you don't know how much time is actually used by your process. All you know is that your process started at time 'x' and ended at time 'y'. During this time, the OS may have switched to running some other processes and there is no way for you to know how much time the other processes took, so you can subtract them out. If you're not running too many other things though, then using the real time clock should give a fairly accurate value.

On the other hand, the clock() function gives an approximate time used by the CPU for your program alone. It also does not include the time your program waited for I/O processes, or other external programs to complete. It also does not include the time used by other processes that had nothing to do with your program. The only trouble with this is, that the resolution is not too high. Unlike what TechNoFear says, the value of CLK_TCK and CLOCKS_PER_SEC are not the same (and not always 1000 either). In fact, this value is highly OS dependent. These are the values for different OS's that I investigated:

FreeBSD 4.6/4.7 - 128 for CLK_TCK and CLOCKS_PER_SEC
OpenBSD 3.2 - 100 for CLK_TCK and CLOCKS_PER_SEC
RedHat Linux 7.1 - 100 for CLK_TCK, 1000000 for CLOCKS_PER_SEC

The man page for clock() on RedHat Linux notes that you're supposed to divide by CLOCKS_PER_SEC to get the number of seconds used. It also notes that POSIX requires that CLOCKS_PER_SEC equals 1000000 independent of the actual resolu_tion.

Reply With Quote
  #5  
Old January 9th, 2003, 10:11 PM
TechNoFear TechNoFear is offline
Offensive Member
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2002
Location: in the perfect world
Posts: 618 TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 13 h 50 m
Reputation Power: 26
Sorry, seems I was not clear enough. When I said
Code:
Under window OS timer messages are the lowest prority

I assumed you would read the rest as MS OS dependant.

Sorry if this caused confusion.

Under MS OS GetTickCount() may be better as it is the number of milliseconds elaped since booting (so will give actual tme not CPU time).



I looked also at the task to be timed

normalSearch(searchString, testArray);

and assumed it was a simple string find function and so would need around a 5 millsecond accurate timer, which is virtually impossible under MS OS without looking at the CPU's cycles.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > How to calculate duration of a process in seconds

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap