Something I have been puzzled about is this: how exactly does a computer regulate and tell time? For example: if I were to write a program that did this:

Do 2+2 then wait 5 seconds

How does the processor know what "5 seconds" is? How is time measured in computer systems? Is there a specific chip for that sole purpose? How does it work?

Thanks for any replies; I'm really interested in computer science, and would love any help you could give me =D.


While Joel's answer is correct, in reality, it's a bit more complicated.

First thing which needs to be taken into consideration (and I'm going to focus only on PCs here) is that there are several clocks in a computer and each has its own use.

Most popular and easiest to understand is the real-time clock. It is basically a chip which has a simple clock inside. They usually have same type of quartz crystals as standard clocks and usually have a battery for time keeping when the computer is powered down. The problem with them is that they aren't very accurate, as can be seen from Syntech's links. The 32.768 kHz crystal is too slow for any timekeeping on modern systems whose processors are in megahertz and gigahertz range.

Here we come to the next point: There are internal clocks used for precise time measurements and countdowns.

A simple clock is programmable interval timer. What it does is wait a certain amount of time and then send an interrupt to the CPU. When the CPU receives the interrupt, it will stop whatever it is doing and tend to the task which generated the interrupt. This way the CPU does not have to constantly check if something is done. Instead it can focus on other jobs and have the PIT tell it when the job is done. The PIT uses 1.193182 MHz clock source and is therefore much more precise than simple RTC.

Next interesting measurement system is time stamp counter. The idea behind it is that we can get much more precise measurements of time using processor's clock source that using various system timers. PIT has 1.193182 MHz clock, but even the earliest x86 processors had much higher clock. So we'll have a timer which is updated after every set amount of processor cycles. At the time processors had very stable clocks and use of TSC was a nice way to make precise time measurements. Use of TSC however brings a number of problems. Different processors have different tick rates and measure time at different speeds. Later on, as techniology advanced we got modern processors which can change their frequency. That is a major problem, sicen the CPU clock isn't constant anymore and we can't use it to measure time.

And that's why we have high precision event timers now. HPET uses a 10 MHz clock and is therefore more precise than PIT. On the other hand, its clock source does not depend on CPU's clock and it can be used to measure time even if CPU's clock changes. Unlike PIT, which works as countdown, HPET measures time since the computer was turned on and compares current time to time when an action is needed.

There are other time sources available for computers which I believe need to be mentioned. Some computers are connected to atomic clocks and can use them to precisely measure time.

A less expensive option and much more common is to use external time source to calibrate internal time sources of the computer. For example GPS receivers can be used to provide high precision time measurements, because GPS satellites have their internal atomic clocks.

Another option which is less common than GPS receiver is to use a special radio receiver which decodes time information from time keeping radio stations such as DCF77 for example. Such time stations have their own high precision time sources and transmit their output over radio. Since radio waves travel at speed of light, the delay is often insignificant.

|improve this answer|||||
  • 1
    Great answer. I didn't know the GPS satellites had their own atomic clocks onboard. Turns out, each of the 24 has 4 atomics clocks each according to this article: science.nasa.gov/science-news/science-at-nasa/2004/… – deltaray Mar 6 '11 at 1:48
  • 2
    Very detailed answer! Thanks for taking the time to write that all out! – DRK3 Mar 6 '11 at 6:07
  • 1
    Especially interesting is the bit about how clocks were reliant on the CPU clock speed, but seeing as how CPU clock speeds are variable today, that becomes an issue. Thanks again. – DRK3 Mar 6 '11 at 23:19
  • @DRK3 Here's a great video on atomic clocks: wimp.com/atomicclock – Griffin Jul 27 '12 at 7:16
  • 1
    This answer somewhat confuses the notions of "accuracy", "precision", and "resolution". In the language of instrumentation, "precision" refers to the repeatability of a measurement. An instrument can be very precise without being accurate, if it hasn't been calibrated properly. (However, it can't be accurate if it isn't precise.) And just because HPET ticks 10 million times a second - hence has higher resolution - doesn't guarantee that it's more precise than something that ticks every millisecond or so. – Jamie Hanrahan Jan 23 '15 at 21:54

IIRC, there's a small crystal the vibrates at a specific frequency when an electric current is passed through it. Each movement is counted and a specific number of them trigger a clock cycle.

|improve this answer|||||
  • 3
    I believe that it is still quartz. (You may even be able to see the little, metal, rounded rectangle stamped “14MHz” or “14.3MHz”—or some other frequency—containing a quartz crystal on the motherboard.) – Synetech Mar 5 '11 at 5:46
  • Thanks for the answer! I had no idea that a crystal (quartz) is used. – DRK3 Mar 6 '11 at 6:06
  • 1
    They don't always use a quartz crystal. Some very inexpensive microcontrollers use a "ceramic resonator" instead. Even the quartz crystal-driven timing on commodity motherboards is not all that precise (i.e. its timing can vary). They are generally no better than a typical digital watch. – Jamie Hanrahan Jan 23 '15 at 21:57
  • Very simple answer! Is this what happens too in quartz watches? – Nicolas Barbulesco Dec 19 '15 at 21:06

HPET uses a 10 MHz clock

HPET uses at leaast a 10 Mhz clock. It can be more precise than 10 Mhz but never less.


|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.