954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to find execution time milliseconds

Respected Sir/Madam

I need to find out the execution time of a fragment of code.
For that I am using
time_t
structure, but it gives time in seconds.
And the value of CLOCKS_PER_SEC in my comp is 1000000.
Will you please me in getting the time in milliseconds

Thanks & regards
Srishekh

srishekh
Newbie Poster
8 posts since Dec 2005
Reputation Points: 10
Solved Threads: 0
 

123456 microseconds equals how many milliseconds?

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

It gives you the time in seconds and you need to know how many milliseconds? Well a millisecond is a thousandth of a second ... one thousand milliseconds in a second. Did I misunderstand your question??

cscgal
The Queen of DaniWeb
Administrator
19,422 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 

Respected Sir/madam
You misunderstood my question.
When it takes time greater than or equal to 1 second it gives
the correct output,but when it takes time less than 1 second
it gives the 0 as the answer.

For example, if the code takes 0.00045 seconds, the output
must be 0.00045,
How to get such a precesion.

srishekh
Newbie Poster
8 posts since Dec 2005
Reputation Points: 10
Solved Threads: 0
 

I maybe wrong, but I remember reading in a Visual Studio 6.0 book that the best resolution possible in it is 1 millisecond. So if that is the case you wont be able to measure 0.00045 seconds. It will come as 0.

PS.
Isn't the greeting "Respected Sir/madam" a bit out of date? It drives me nuts.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

The functions in time.h are only accurate to 1 second -- so no matter how you divide it you still have only 1 second accuracy. You need to use other functions, such as clock(), or win32 api function GetTickCount(), or GetSystemTime() that provide more accuracy.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
The functions in time.h are only accurate to 1 second -- so no matter how you divide it you still have only 1 second accuracy. You need to use other functions, such as clock(), or win32 api function GetTickCount(), or GetSystemTime() that provide more accuracy.


Do you know the resolution of those functions. As I said earlier I remember that Clock() had only a resolution of 1ms.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

they all have resolution of about 1 ms. There are high resolution counters

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

If you are in Unix:
setitimer() and getitimer() have microsecond resolution on POSIX-compliant systems.

Do you know about profiling and profilers?

jim mcnamara
Junior Poster
180 posts since May 2004
Reputation Points: 62
Solved Threads: 10
 
I need to find out the execution time of a fragment of code.

You don't need finer resolution timing. If you want to measure the thickness of a piece of paper, would you buy an expensive microscope, or would you measure a stack of 200 pieces of paper and then divide by 200?

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 
etechsupport
Posting Whiz in Training
208 posts since Sep 2005
Reputation Points: 12
Solved Threads: 2
 

i don't think you can use the concept of 200 pages to measure time accurately.
let us suppose the width of a paper is 1/10 th of mm and you have a scale which can measure only upto a mm. suppose you end up measuring 209 pages with the scale, than you'll end up with a reading that'llgive you the width of each page as .95 = 20/209.
The problem becomes more acute in computing where instructions take around a nano sec to complete.

lots of platform specific api. On windows this works
GetTickCount() . gives the number of ms since midnight

perseus31
Newbie Poster
1 post since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

The most precise unit is as precise as you can get, so Rashakil's analogy does not make sense to me. You can get less precise, but not more precise.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 
You don't need finer resolution timing. If you want to measure the thickness of a piece of paper, would you buy an expensive microscope, or would you measure a stack of 200 pieces of paper and then divide by 200?

In other words, run it a whole bunch of times until it runs to 10 seconds, then divide 10 by the number of times it ran. Now, of course, all that counting and looping will throw off your results -- after all, that code and all the calls to time() also take time. So you'll be off only by 5-15%.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
i don't think you can use the concept of 200 pages to measure time accurately.

but you can wait 3 1/2 years to respond to a thread and divide that time by the number of total posts, for a rough estimate of the number of thread-posts per year.

amirite?

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

uRrite. Just another case of some turkey not caring that 100 pages in on a forum might be moot thread. Nothing better to do, obviously.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You