-1

In C++ I wrote:

time_t tmp1, tmp2;
time(&tmp1);
sleep(1);
time(&tmp2);
std::cout << tmp2-tmp1;

But every-time I run it I see 1 printed, why the result is too perfect? shouldn't it contain some milliseconds too?

  • Consider using the libraries from `#include ` to get a timer with milliseconds or microseconds. At the botton of https://en.cppreference.com/w/cpp/chrono there's an example. – Bill Lynch Jul 19 '21 at 01:18
  • If `time_t` is more precise than integer count of seconds, use [`difftime()`](https://www.cplusplus.com/reference/ctime/difftime/). – chux - Reinstate Monica Jul 19 '21 at 01:38
  • The `time` function returns an absolute number of seconds (integer), never a floating point value. – selbie Jul 19 '21 at 01:42
  • `auto t1 = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()); sleep(1); auto t2 = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()); std::cout<<(t2 - t1).count();` Try this. – Yves Jul 19 '21 at 01:58

2 Answers2

2

I recommend using std::chrono::steady_clock to time a function:

auto const start       = std::chrono::steady_clock::now();
f();
auto const stop        = std::chrono::steady_clock::now();
auto const duration    = stop-start;
auto const duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration);
//                                                               ^^^^^^^^^^^^

std::cout << duration_ms.count();
Andreas DM
  • 10,685
  • 6
  • 35
  • 62
  • Great answer, though I think `std::chrono::steady_clock` may be preferable for measuring the time of an operation because it is guaranteed not to be modified. – Camwin Jul 19 '21 at 21:58
  • @Camwin You are right! I intended to write steady_clock but my fingers were typing system_clock – Andreas DM Jul 20 '21 at 09:35
1

Your system, like many C++ systems, follows the POSIX specification for time_t and time. The POSIX specification says that a time_t will contain the number of seconds since 1/1/1970.

See here for more.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • What if I want something accurate to use for time of insertion (I want to know in A was inserted before B in some data structure) what should I use in this case? –  Jul 19 '21 at 01:19
  • without using chrono –  Jul 19 '21 at 01:20
  • 1
    @John If your platform has [gettimeofday](https://pubs.opengroup.org/onlinepubs/009604599/functions/gettimeofday.html) you can use that. – David Schwartz Jul 19 '21 at 01:21
  • Still this prints same for both, I want something really accurate: ```timeval tim{}; gettimeofday(&tim,nullptr); timeval tim2{}; gettimeofday(&tim2,nullptr); std::cout << tim.tv_sec << std::endl; std::cout << tim.tv_usec << std::endl; std::cout << tim2.tv_sec << std::endl; std::cout << tim2.tv_usec << std::endl;``` –  Jul 19 '21 at 01:27
  • output: ```1626658021 697993 1626658021 697993``` –  Jul 19 '21 at 01:28
  • @John Your platform may not have anything that accurate. What platform are you using? – David Schwartz Jul 19 '21 at 01:37
  • @John There is no call to `sleep` in the code in your comment. As a result, everything happens in less than a microsecond. With the call to `sleep` in place, you do see a difference, see: https://wandbox.org/permlink/9BWbIOpBFTwtiRhm – Paul Sanders Jul 19 '21 at 01:41