Solved

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

Posted on 2013-12-08
5
9,004 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 32

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 32

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now