[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Display time in milliseconds in C++/C linux /unix

Posted on 2013-12-08
5
Medium Priority
?
11,291 Views
Last Modified: 2013-12-12
Hi guys,

I need help on how to display time in milliseconds  in c/c++ under linux ?
0
Comment
Question by:bachra04
  • 2
  • 2
5 Comments
 
LVL 35

Expert Comment

by:sarabande
ID: 39705505
you would use the gettimeofday function:

struct timeval  tv = { 0 };
gettimeofday(&tv, NULL);

double mill = (tv.tv_usec) / 1000 ; 

Open in new window


Sara
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 1000 total points
ID: 39706212
If you need a bit more accuracy, use 'clock_gettime()' (http://linux.die.net/man/2/clock_gettime). You'll get a

struct timespec {
    time_t   tv_sec;        /* seconds */
    long     tv_nsec;       /* nanoseconds */
};

Open in new window


See e.g. http://www.guyrutenberg.com/2007/09/22/profiling-code-using-clock_gettime/ ("Profiling Code Using clock_gettime"):

#include <time.h>

// ...

	struct timespec gettime_now;

	clock_gettime(CLOCK_REALTIME, &gettime_now);

Open in new window

0
 
LVL 2

Author Comment

by:bachra04
ID: 39706398
The problem I have seen is how to printf the value, I'm seeing weird values
267.0000
948.0000

then
15.0000


etc ...
I'm expecting that time of day will be increasing
0
 
LVL 86

Expert Comment

by:jkr
ID: 39706480
For both system calls? The man page to gettimeofday (http://linux.die.net/man/2/gettimeofday) states:

POSIX.1-2008 marks gettimeofday() as obsolete, recommending the use of clock_gettime(2) instead. [...] The time returned by gettimeofday() is affected by discontinuous jumps in the system time (e.g., if the system administrator manually changes the system time). If you need a monotonically increasing clock, see clock_gettime(2).

(even though I wouldn't expect such a weird behaviour)
0
 
LVL 35

Accepted Solution

by:
sarabande earned 1000 total points
ID: 39706670
gettimeofday and clock_gettime have same accuracy. it is for Linux systems normally better than 1 millisecond (while for windows it is 15 ms). higher accuracy you only get by using high performance counters which is hardware of your mainboard if available and has a own api.

the numbers you showed are milliseconds expressed as doubles. if you use an int at left side of the calculation, the results should print correctly.

int mill = (int)(tv.tv_usec/ 1000);

Open in new window


if you want the seconds as hours, minutes and seconds you may do:

int hours = (int)(tv.tv_sec/3600);
int minutes = (int)((tv.tv_sec%3600)/60);
int seconds = (int)(tv.tv_sec%60);

Open in new window


to print it properly you may include <sstream> and <iomanip> headers and do like

std::ostringstream oss;
oss << std::right << std::setw(2) << std::setfill('0') << hours << ':'
       << std::right << std::setw(2) << std::setfill('0') << minutes << ':'
       << std::right << std::setw(2) << std::setfill('0') << seconds << '.'
       << std::right << std::setw(3) << std::setfill('0') << mill;
std::cout << oss.str();

Open in new window


Sara
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question