Solved

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

Posted on 2013-12-08
5
9,702 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 34

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 250 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 34

Accepted Solution

by:
sarabande earned 250 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

730 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