如何以毫秒为单位以c++;? #包括 #包括 #包括 #包括 使用名称空间std; 使用namesapce-chrono; int main(){ int f; 开始、结束的时间; 时间(&开始); cin>>f; 时间(&结束); 双dif=difftime(结束,开始); printf(“经过的时间是%.2lf秒。”,dif); } 大家好,我现在正在做C++任务,我基本上需要用户在10秒内输入一些东西。我设法找出了如何以秒为单位计算时间,但我需要以毫秒为单位,因为我必须找出10秒以上经过了多少毫秒。我不是那么有经验的C++,将非常感谢任何建议,可能有助于引导我在正确的方向。非常感谢诸如此类的事情 #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 ); } #包括 #包括 自动启动(std::chrono::high_resolution_clock::now()); //代码。。。 自动结束(std::chrono::高分辨率时钟::now()); 自动持续时间(std::chrono::duration_cast(结束-开始)); 标准::cout

如何以毫秒为单位以c++;? #包括 #包括 #包括 #包括 使用名称空间std; 使用namesapce-chrono; int main(){ int f; 开始、结束的时间; 时间(&开始); cin>>f; 时间(&结束); 双dif=difftime(结束,开始); printf(“经过的时间是%.2lf秒。”,dif); } 大家好,我现在正在做C++任务,我基本上需要用户在10秒内输入一些东西。我设法找出了如何以秒为单位计算时间,但我需要以毫秒为单位,因为我必须找出10秒以上经过了多少毫秒。我不是那么有经验的C++,将非常感谢任何建议,可能有助于引导我在正确的方向。非常感谢诸如此类的事情 #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 ); } #包括 #包括 自动启动(std::chrono::high_resolution_clock::now()); //代码。。。 自动结束(std::chrono::高分辨率时钟::now()); 自动持续时间(std::chrono::duration_cast(结束-开始)); 标准::cout,c++,c++11,time,clock,chrono,C++,C++11,Time,Clock,Chrono,在C++11和更新的标准修订版中: #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::mill

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

#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";
#包括
使用名称空间std::chrono;
自动启动=高分辨率时钟::现在();
//测量的东西
自动结束=高分辨率时钟::现在();
持续时间差异=结束-开始;//这是用记号表示的
毫秒d=持续时间(差异);//滴答滴答

std::cout下面的完整程序显示了如何通过使用C++11中添加的
std::chrono
工具来实现这一点:

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;
}
提供您期望的结果:

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.
#包括
自动启动时间(std::chrono::高分辨率时钟::now());
//你想做什么就做什么。
自动结束时间(std::chrono::high_resolution_clock::now());
自动持续时间(标准:计时:持续时间)
(endTime-startTime));
auto elapsedMs=持续时间。计数();

相关:
#include
是正确的起点。为什么要在同一代码中使用
cin
printf
?这只是Jekyll和Hyde的东西,是的,
printf
是Hyde位:-)你的意思是在超时机制内,10秒后默认为?
#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;
}
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();