Getting system time with high precision

L

Leigh Sharpe

Hi all,
Is there any way of getting the system time, right down to the microsecond,
or even millisecond?

I need to do something a little like this:

int start_time=getTheTimeSomehow(); // Get the current time
doSomethingElse(); // Go and do something
else.
int end_time=getTheTimeSomehow() // What's the time now?
int elapsed_time=end_time-start_time; // How long did it take?

And I need to know the time as accurately as possible.


I would expect there to be a function somewhere, but I just don't know
where.
 
S

Sarath

Hi all,
Is there any way of getting the system time, right down to the microsecond,
or even millisecond?

I need to do something a little like this:

int start_time=getTheTimeSomehow(); // Get the current time
doSomethingElse(); // Go and do something
else.
int end_time=getTheTimeSomehow() // What's the time now?
int elapsed_time=end_time-start_time; // How long did it take?

And I need to know the time as accurately as possible.

I would expect there to be a function somewhere, but I just don't know
where.

Check the following link for sample snippet if you using Visual C++

http://msdn.microsoft.com/library/d.../vccore98/HTML/_crt_strftime.2c_.wcsftime.asp
 
I

Ian Collins

Leigh said:
Hi all,
Is there any way of getting the system time, right down to the microsecond,
or even millisecond?
Very likely, but not in a portable way, try an operating system specific
group.
 
S

Sherm Pendley

Leigh Sharpe said:
Is there any way of getting the system time, right down to the microsecond,
or even millisecond?

Not in standard C++. There may be OS-specific functions that provide that
capability for your OS, but those wouldn't be on-topic for this group. I'd
suggest asking in a group that's focused on the capabilities of the OS you're
writing for.

sherm--
 
L

Leigh Sharpe

I'd
suggest asking in a group that's focused on the capabilities of the OS you're
writing for.


OK. That's the second response along those lines. Which prompts the
question, which group would that be? My target platform is Linux.
 
L

liam_herron

On windows I would use:
QueryPerformanceCounter located in <windows.h>

On Linux I would use:
::gettimeofday located in <sys/time.h>

example Linux Use:

#include <sys/time.h>

const long long TICKS_PER_SECOND = 1000000L;

struct timeval tv;
struct timezone tz;
::gettimeofday(&tv, &tz);
long long quadPart = tv.tv_sec * TICKS_PER_SECOND + tv.tv_usec;

Hope that helps,
Liam
 
L

Larry Smith

Leigh said:
OK. That's the second response along those lines. Which prompts the
question, which group would that be? My target platform is Linux.

Try one of these:

comp.os.linux.development.system
comp.os.linux.development.apps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top