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.
You want to use the time() function. This
code snippet will do what you want:
startTime = time(NULL);
// do some long process...
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
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
Email them at
to inquire about support for your project.