[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2013-12-08
5
Medium Priority
?
10,858 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 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

649 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