2

This program returns the time in seconds. I need to return it in microseconds.

time_t timeStart = time(0);
usleep(2.5e6);
time_t timeEnd = time(0);

float R = (timeEnd - timeStart);
std::cout << R << std::endl;
kalehmann
  • 4,821
  • 6
  • 26
  • 36
Strife
  • 39
  • 1
  • 1
  • 3

3 Answers3

6

If you're looking for a higher resolution, you can use std::chrono::high_resolution_clock from C++11.

#include <chrono>

using namespace std::chrono;

high_resolution_clock::time_point t1 = high_resolution_clock::now();

/* some extensive action... */
usleep(2.5e6);

high_resolution_clock::time_point t2 = high_resolution_clock::now();

duration<double> time_span = duration_cast<duration<double>>(t2 - t1);

std::cout << "It took me " << time_span.count() << " seconds.";
std::cout << std::endl;

Outputs something like

It took me 0.091001 seconds.

Example from http://www.cplusplus.com/reference/chrono/high_resolution_clock/now/

Lukas W
  • 305
  • 1
  • 9
  • thanks very much, but I have a problem with my compilator (Kate+Terminal). I need c++11. – Strife Jun 15 '15 at 16:54
  • @Strife I suppose you're using GCC/Clang. You'll probably want to add `-std=c++0x` to the build options to enable C++11 support. – Lukas W Jun 15 '15 at 22:42
  • For the Windows platform, you should avoid using ``std::chrono::high_resolution_clock`` with VS 2012/2013 due to a [known issue](https://connect.microsoft.com/VisualStudio/feedback/details/719443/) and use ``QueryPerformanceCounter`` instead. This is fixed with VS 2015. – Chuck Walbourn Jun 16 '15 at 05:27
  • @Strife Please accept an answer and up-vote answers/comments that were helpful. See e.g. http://stackoverflow.com/help/someone-answers for reference. – Lukas W Jun 16 '15 at 17:50
  • 1
    Lukas, please remove the last s in the first line of code, it should end with std::chrono. Apart from that: thanks! ps. if you'd add #include I think it would be more complete, but that's maybe just personal. – ikku100 Apr 15 '16 at 08:35
  • You can use `auto` instead of explicitly declaring the types. – L. F. Aug 22 '19 at 20:40
3

using function gettimeofday

#include <sys/time.h>

int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv, const struct timezone *tz);

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

settimeofday(): _BSD_SOURCE

Description

The functions gettimeofday() and settimeofday() can get and set the time as well as a timezone. The tv argument is a struct timeval (as specified in ):

struct timeval {
    time_t      tv_sec;     /* seconds */
    suseconds_t tv_usec;    /* microseconds */
};

and gives the number of seconds and microseconds since the Epoch (see time(2)). The tz argument is a struct timezone:

struct timezone {
    int tz_minuteswest;     /* minutes west of Greenwich */
    int tz_dsttime;         /* type of DST correction */
};

this function can get microsecond

kalehmann
  • 4,821
  • 6
  • 26
  • 36
Liyuan Liu
  • 172
  • 2
2

Take a look at the std::chrono header. It contains many different options for time manipulation, including the ability to convert to microseconds via std::chrono::duration_cast.

lcs
  • 4,227
  • 17
  • 36