如何在c ++中输出以毫秒为单位的时间?

时间:2018-03-28 22:39:17

标签: c++ c++11 time clock chrono

#include <iostream>
#include <chrono>
#include <time.h>
#include <stdio.h>

using namespace std;
using namesapce chrono;

int main() {
  int f;
  time_t start, end;
  time (&start);
  cin >> f;
  time (&end);
  double dif = difftime (end, start);
  printf ("Elapsed time is %.2lf seconds.", dif );
}

大家好,我目前正在进行C ++任务,基本上我需要在10秒内让用户输入内容。我设法找出如何计算秒的时间,但我需要它是毫秒,因为我必须找出10秒以上的毫秒数。我不熟悉C ++,并且非常感谢任何可能有助于引导我朝着正确方向前进的建议。非常感谢

3 个答案:

答案 0 :(得分:5)

这些方面的东西......

#include <iostream>
#include <chrono>
auto start(std::chrono::high_resolution_clock::now());

// Code...

auto end(std::chrono::high_resolution_clock::now());
auto duration(std::chrono::duration_cast<std::chrono::milliseconds>(end - start));
std::cout << "Duration: " << duration.count() << " ms\n";

答案 1 :(得分:5)

在C ++ 11和更新的标准修订中:

#include <chrono>
using namespace std::chrono;

auto start = high_resolution_clock::now();
  // something to measure
auto end = high_resolution_clock::now();
duration<double> diff = end - start; // this is in ticks
milliseconds d = duration_cast<milliseconds>(diff); // ticks to time

std::cout << diff.count() << "s\n";
std::cout << d.count() << "ms\n";

之前的那个:

<sys/time.h>

struct timeval tp;
gettimeofday(&tp, NULL);
long int ms = tp.tv_sec * 1000 + tp.tv_usec / 1000;

此外,您可以使用此简单代码段代码来对代码块进行基准测试:

using namespace std::chrono;

class benchmark {
  public:
  time_point<high_resolution_clock>  t0, t1;
  unsigned int *d;
  benchmark(unsigned int *res) : d(res) { 
                 t0 = high_resolution_clock::now();
  }
  ~benchmark() { t1 = high_resolution_clock::now();
                  milliseconds dur = duration_cast<milliseconds>(t1 - t0);
                  *d = dur.count();
  }
};

// one way to use it can be :
#define BENCH(TITLE,CODEBLOCK) \
  unsigned int __time__##__LINE__ = 0;  \
  { benchmark bench(&__time__##__LINE__); \
      CODEBLOCK \
  } \
  printf("%s took %dms\n",(TITLE),__time__##__LINE__);


int main(void) {
  BENCH("TITLE",{
    for(int n = 0; n < testcount; n++ )
      int a = n % 3;
  });
  return 0;
}

答案 2 :(得分:1)

以下完整的程序通过使用C ++ 11中添加的std::chrono工具来说明如何做到这一点:

#include <iostream>
#include <chrono>
#include <thread>

int main(int argc, char *argv[]) {
    // Check args.

    if (argc != 2) {
        std::cerr << "Usage: testprog <sleepTime>" << std::endl;
        return 1;
    }

    // Create a millisecond sleep time from argument.

    auto sleepTime = strtoul(argv[1], nullptr, 10);
    sleepTime = sleepTime * 1234 + 1000;
    std::cout << "Given '" << argv[1] <<
        "', should sleep for about " << sleepTime <<
        "ms ... " << std::flush;

    // Get the start time, then wait for a bit.

    auto startTime(std::chrono::high_resolution_clock::now());

    std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));

    // Get end time, work out and print duration.

    auto endTime(std::chrono::high_resolution_clock::now());
    auto duration(std::chrono::duration_cast<std::chrono::milliseconds>
        (endTime - startTime));
    std::cout << "that took " << duration.count() << "ms." << std::endl;
}

使用以下bash测试命令运行此命令:

for i in {0..10} ; do ./testprog $i ; done

为您提供您期望的结果:

Given '0', should sleep for about 1000ms ... that took 1000ms.
Given '1', should sleep for about 2234ms ... that took 2235ms.
Given '2', should sleep for about 3468ms ... that took 3469ms.
Given '3', should sleep for about 4702ms ... that took 4703ms.
Given '4', should sleep for about 5936ms ... that took 5937ms.
Given '5', should sleep for about 7170ms ... that took 7170ms.
Given '6', should sleep for about 8404ms ... that took 8404ms.
Given '7', should sleep for about 9638ms ... that took 9638ms.
Given '8', should sleep for about 10872ms ... that took 10872ms.
Given '9', should sleep for about 12106ms ... that took 12106ms.
Given '10', should sleep for about 13340ms ... that took 13340ms.

该代码中的重要行实际上只是获取开始和结束时间点的行,然后计算它们之间的持续时间。他们可以归结为:

#include <chrono>

auto startTime(std::chrono::high_resolution_clock::now());

// Do whatever you want to time.

auto endTime(std::chrono::high_resolution_clock::now());
auto duration(std::chrono::duration_cast<std::chrono::milliseconds>
    (endTime - startTime));
auto elapsedMs = duration.count();