The Database Managers, Inc.

Contact The Database Managers, Inc.


Use an RSS enabled news reader to read these articles.Use an RSS enabled news reader to read these articles.

Timer with a 1 second resolution

by Curtis Krauskopf
Q:  I need a simple timer that can track the number of seconds between two events.

A:
You want to use the time() function.  This code snippet will do what you want:

#include <time.h>
...
...
  time_t startTime;
  startTime = time(NULL);
  // do some long process...
  time_t stopTime;
  stopTime = time(NULL);
  time_t elapsedTime = stopTime - startTime;
  printf("The number elapsed seconds is %ld",elapsedTime);
...

The time() function returns a long integer that represents the number of seconds since midnight of January 1, 1970.  By simply subtracting the value of the beginning time from the current time, you will have the number of elapsed seconds.

A much more accurate timer with a resolution better than 1,000,000th of a second is available on Windows systems by using a Windows API. 


Popular C++ topics at The Database Managers:

 


The Database Managers has been providing custom programming services in since 1985. The types of projects they have worked on ranges from automated inventory control systems to intelligent systems that detect fraud in insurance claims.

Email them at to inquire about support for your project.

C++ FAQ Services | Programming | Contact Us | Recent Updates
Send feedback to: