C++ 跟踪自程序启动以来经过了多少秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1735307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:03:37  来源:igfitidea点击:

C++ Keeping track of how many seconds has passed since start of program

c++time

提问by Noob

I am writing a program that will be used on a Solaris machine. I need a way of keeping track of how many seconds has passed since the start of the program. I'm talking very simple here. For example I would have an int seconds = 0; but how would I go about updating the seconds variable as each second passes?

我正在编写一个将在 Solaris 机器上使用的程序。我需要一种方法来跟踪自程序启动以来经过了多少秒。我这里说的很简单。例如我会有一个 int seconds = 0; 但是我将如何在每一秒过去时更新 seconds 变量?

It seems that some of the various time functions that I've looked at only work on Windows machines, so I'm just not sure.

我看过的一些不同的时间函数似乎只适用于 Windows 机器,所以我不确定。

Any suggestions would be appreciated.

任何建议,将不胜感激。

Thanks for your time.

谢谢你的时间。

回答by Michael Burr

A very simple method:

一个非常简单的方法:

#include <time.h>
time_t start = time(0);

double seconds_since_start = difftime( time(0), start);

The main drawback to this is that you have to poll for the updates. You'll need platform support or some other lib/framework to do this on an event basis.

这样做的主要缺点是您必须轮询更新。您需要平台支持或其他一些库/框架才能在事件基础上执行此操作。

回答by Casper Beyer

Use std::chrono.

使用std::chrono

#include <chrono>
#include <iostream>

int main(int argc, char *argv[])
{
   auto start_time = std::chrono::high_resolution_clock::now();
   auto current_time = std::chrono::high_resolution_clock::now();

   std::cout << "Program has been running for " << std::chrono::duration_cast<std::chrono::seconds>(current_time - start_time).count() << " seconds" << std::endl;

   return 0;
}

If you only need a resolution of seconds, then std::steady_clockshould be sufficient.

如果您只需要秒的分辨率,那么std::steady_clock就足够了。

回答by prismofeverything

You are approaching it backwards. Instead of having a variable you have to worry about updating every second, just initialize a variable on program start with the current time, and then whenever you need to know how many seconds have elapsed, you subtract the now current time from that initial time. Much less overhead that way, and no need to nurse some timing related variable update.

你正在向后接近它。您不必担心每秒更新一个变量,只需在程序开始时用当前时间初始化一个变量,然后每当您需要知道已经过去了多少秒时,您就可以从该初始时间中减去现在的当前时间。这种方式的开销要少得多,而且不需要处理一些与时间相关的变量更新。

回答by rplusg

#include <stdio.h>
#include <time.h>
#include <windows.h>
using namespace std;
void wait ( int seconds );
int main ()
{
  time_t start, end;
  double diff;
  time (&start); //useful call
  for (int i=0;i<10;i++) //this loop is useless, just to pass some time.
  {
  printf ("%s\n", ctime(&start));
  wait(1);
  }
  time (&end);//useful call

  diff = difftime(end,start);//this will give you time spent between those two calls.
  printf("difference in seconds=%f",diff); //convert secs as u like
  system("pause");
  return 0;
}
void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

this should work fine on solaris/unix also, just remove win refs

这也应该在 solaris/unix 上正常工作,只需删除 win refs

回答by LeffeBrune

You just need to store the date/time when application started. Whenever you need to display for how long your program is running get current date/time and subtract the when application started.

您只需要存储应用程序启动的日期/时间。每当您需要显示程序运行的时间时,获取当前日期/时间并减去应用程序启动的时间。