I would like to write software that can reliably get the "relative physical time", with the highest resolution, and lowest overhead as possible.

By "relative physical time", I mean, for example, the actual amount of physical time that has passed in the physical world since the computer was turned on. It doesn't need to be relative to some external time system, calendar, or event.

While there is a plethora of information about this, I think it is a topic this is misunderstood by SW people (including myself), and thus, lot's of misinformation. Hopefully an electrical or computer engineer is better informed.

Do computers (ex. iPhones, game consoles, personal PC's) come equipped with hardware to do this? Or, are there trade offs that need to be made: (i.e. if we want high res + low overhead, use a time step counter (ex. RDTSC), but no longer "wall time").

  • 5
    $\begingroup$ "highest resolution and lowest overhead possible" is no spec at all. $\endgroup$ – Olin Lathrop Dec 9 '16 at 11:58
  • $\begingroup$ Define "real time" first! I take exception with the current answers that define real time in terms of any internal "clock" (actually a pulse generator). If you want true time-base reference then you need to tie into one of the online timebase services which refer back to the NIST clocks. $\endgroup$ – Carl Witthoft Dec 9 '16 at 14:16
  • $\begingroup$ @CarlWitthoft given the context I think it's reasonable that "real time" in this situation means time from a real time clock rather than something like system up time. He makes no reference in the question to absolute or authoritative time sources. While you may personally feel that calling such time sources real time is inaccurate it is an industry standard naming convention. $\endgroup$ – Andrew Dec 9 '16 at 16:01
  • $\begingroup$ @Andrew Yes, when I say "real-time", I just mean that, if t seconds passed in the physical world, the clock reports t seconds passed. I don't care about relative accuracy to any authoritative absolute time system. $\endgroup$ – anon Dec 12 '16 at 7:53
  • $\begingroup$ @OlinLathrop Sorry :) I'm not quite sure how to define a "spec" for it :) I only care about relative time - don't care if that time is synced to some external authoritative time source. For example: all I care about is being able to measure is physical time that passed since the computer turned on (not "calendar time"). I have no idea if that helps $\endgroup$ – anon Dec 12 '16 at 7:55

Most computers / computing devices will have a real time clock (RTC) built in. This is normally included in the processor or motherboard chip set rather than as a separate part. It will be designed such that when the system is on it uses the main system power to run but when the system is off it has a separate power pin that is connected to a small battery.

It will keep track of time to a reasonable level of accuracy (say about 50 ppm (parts per million)) for years running off a watch battery.

The RTC is a low power part and so relatively slow and generally not very high resolution, when running most computer systems will run a separate faster and higher resolution clock that they initialize based on the time from the RTC.

From a software point of view unless you are working on smaller embedded systems then once the system is up and running the underlying OS will include a system for tracking and reporting time to applications. That is probably the best trade off between accuracy and speed that you will have available.
In the absence of any other sources the OS time will be periodically synced in the background to the clock from the RTC. If a more trusted time source becomes available (e.g. the user, NTP, a GPS signal, the signal from a cell tower) then the system may switch to using that and update the value in the RTC.

  • $\begingroup$ Sorry, I should be more precise, I thin. I don't necessary care about syncing to some external time. I just want to be able to know that t physical seconds have passed since the computer was turned on. $\endgroup$ – anon Dec 12 '16 at 7:56
  • $\begingroup$ Time since switch on is entirely dependent on the OS and software running. A clock chip can tell you the time but it can't tell you when the system was turned on. Most OS based systems will have an uptime system call that will give you that information. For embedded or bare bones systems there may be nothing that tracks it, it depends. $\endgroup$ – Andrew Dec 12 '16 at 9:10

There are many ways to track real time. Most general purpose computers include a real time clock, with OS calls to get the time. Small embedded processors can have RTC (real time clock) hardware built-in. RTC chips are also available that can be connected to a processor via IIC and the like.

It is also quite possible to perform the RTC function in firmware. It is often useful for other reasons to have a periodic interrupt, like every 1 ms (1 kHz rate). This can easily be the basis for a real time clock. The accuracy is as good as the oscillator that the 1 ms events are derived from. This can be a crystal, so down to 20 PPM or so is cheaply available. Down to about 5 PPM can be possible with selected (extra money) crystals. Lower requires some sort of stabilization, external oscillator module, or the like.

  • $\begingroup$ I think we're overload the "real" in real time :P. AKAIK, a RTC is a low-power component used to keep track of time even when the computer is powered off, and that initializes system time on startup - but has high overhead and low resolution. There are the also variants of programmable interval timers that an OS can use for say, scheduler interrupts, but not for general time queries. I know there are time step counters - but sometimes these depend on the CPU clock speed, which may change throughout execution - and thus not suitable to measure "physical time" (I should edit question) $\endgroup$ – anon Dec 12 '16 at 8:02
  • $\begingroup$ @anon: "Real time" just means a measure of absolute time, as apposed to something like a counter that measures only relative time. Nothing in the definition of "real time clock" requires that it be low power or that it initialize some other clock on system startup, although those may be common. $\endgroup$ – Olin Lathrop Dec 12 '16 at 11:40

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