Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Time of Day Function - is there one? 1

Status
Not open for further replies.

twitman

MIS
Sep 7, 1999
1
US
I'm looking for a function that will give me the time before a function runs, and then after it is complete, so I can analyze the time it takes for two algorithms to work. I need the output to be in microseconds. Is there a function call in a c++ library that will give me the time of day at a starting point, at the finishing point, subtract the latter from the former and return in microseconds?<br>
<br>
Tom
 
Include the &lt;time.h&gt; library and then call time(). I've seen this function used to provide the seed for random number generators by using srand(time()).
 
You have to use time function like this;<br>
{<br>
time_t starttime, endtime;<br>
time( &starttime );<br>
algo1();<br>
time(&endtime);<br>
printf(&quot;The time taken to execute algo1 is = %ld\n&quot;,endtime-starttime);<br>
...<br>
}<br>
<br>
Does it answer your question ?<br>
Thanx<br>
Siddhartha Singh<br>
<A HREF="mailto:ssingh@aztecsoft.com">ssingh@aztecsoft.com</A><br>

 
hi,<br>can anyone write the solution for this program?<br><br>thanks<br>shilpa<br><br><br>1.Create a class for a TimeRecord object. It must be able to support: <br><br>                           ·        Timing an event to the hundredths of a second (consider clock() ).<br>                           ·        Recording and printing the current date (consider time() and<br>                              asctime() )<br>                           ·        Recording and printing the time of day. (consider time() and<br>                              asctime() )<br><br>        a.Design the class structure. What are the private data members ? What are the public method<br>          functions ? Be sure to include Get/Set functions for each data member. <br>        b.Design any additional method functions that a client might find useful for this class. Describe<br>          their purpose. <br>        c.You do not need to overload any operators for this class. <br><br> <br>         Write a small driver function that tests the most important methods of the class.<br><br>
 
Hi Shilpa and Tom,


Try using the function, gettimeofday(). This obtains the current time, expressed as seconds and microseconds since 00:00 Coordinated Universal Time (UTC).
I think this will helpful than function time().

cheers,
Manjunath P S [sig]<p>P S Manjunath<br><a href=mailto:manjunath.ps@chennaimail.ltitl.com>manjunath.ps@chennaimail.ltitl.com</a><br><a href= > </a><br> [/sig]
 
&quot;time&quot; doesn't give msec.
On SGI I use platform depended functions:
&quot;clock_gettime&quot;
this function returns nsec.

Any calendar function will give you best time approximation. [sig][/sig]
 
Use the clock function. This function returns the clock tickts from the strating of the program.

#include <time.h>
#include <stdio.h>

int main(void)
{
clock_t start, end;
start = clock();
// the process, may ur function call.
end = clock();
printf(&quot;The time was: %d\n&quot;, end - start); // if u want the time in seconds the divid the (end-start)/ CLOCKS_PER_SEC.

return 0;
}

the times function can also be used.

Maniraja S
 
When I try to run the time function in a for loop, it produces the same number everytime. How could I use it such that it produces the current time in seconds or even microseconds everytime?
 
// Here is a snippet from a class static method of mine.
// This will give you current date/time to the millisecond.

#include <unistd.h>
#include <strstream>
#include <iomanip>
#include <cstring>

using namespace std;

// this const was part of a class...
const int getTimeOfDayBufferLength = 27;

// this function was a static method in that class...
char* myGetTimeOfDay(char* buffer, int bufferLength)
{
if (bufferLength<getTimeOfDayBufferLength)
{
buffer[0] = '\0';
}
else
{
timeval tv;
gettimeofday( &tv, NULL );
strftime( buffer, 20,
&quot;%Y/%m/%d %H:%M:%S&quot;, localtime(&tv.tv_sec) );
ostrstream ostr;
ostr << ':' << setfill('0') << setw(6) << tv.tv_usec
<< ends;
char* sp = ostr.str();
strcat(buffer, sp);
delete [] sp;
}
return buffer;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top