C ++跟踪自程序启动以来经过了几秒钟 [英] C++ Keeping track of how many seconds has passed since start of program

查看:57
本文介绍了C ++跟踪自程序启动以来经过了几秒钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个将在Solaris计算机上使用的程序.我需要一种方法来跟踪自程序启动以来经过了几秒钟.我在这里说的很简单.例如,我将有一个int seconds = 0;但是随着秒的过去,我将如何更新seconds变量?

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?

我看过的某些各种时间函数似乎只能在Windows计算机上使用,所以我不确定.

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.

任何建议将不胜感激.

感谢您的时间.

推荐答案

一个非常简单的方法:

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

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

主要缺点是您必须轮询更新.您需要基于事件的平台支持或其他lib/framework.

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.

这篇关于C ++跟踪自程序启动以来经过了几秒钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆