English 中文(简体)
C++ Keeping track of how many seconds has passed since start of program
原标题:
  • 时间:2009-11-14 19:24:23
  •  标签:
  • c++
  • time

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?

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.

Any suggestions would be appreciated.

Thanks for your time.

问题回答

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.

Use 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_clock should be sufficient.

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.

#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
", 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

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.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签