Latency

From Helpful
Jump to navigation Jump to search


What

In a general sense, latency names the lateness of one thing compared to another.


...in the broadest sense:

"the difference between seeing and hearing someone kick a ball halfway across a field may be 100ms"
"the speed of sound means it takes roughly 3 milliseconds to travel a meter"
"if sound is delayed in video more than 30 milliseconds or so, then it looks wrong"
"from starting to pressing a key on your keyboard, to registering in a program, might take 50 milliseconds"
"from presenting a frame for the monitor to it emitting all that as light will may take 10 milliseconds"
"asking a computer to play a sound might be sound in air at least 40 milliseconds later, or 10ms or 200ms, it depends"

...are different ideas worded in terms of latency.


In a practical sense, latency only really matters whenever there is something to be late to - another reference, that we actively care about for whatever reason.

Say,

  • when you are playing music to relax, if between pressing 'play' and you starting to hear anything it takes 300ms, you would barely notice. It could start a second late and you would notice, but still not care.
    • yet if you were playing a music video, then if the audio is early or late relative to the video by more than 30ms or so, you will start to notice that mouth movements are wrong.
  • keypresses on a simple keyboard probably arrive in a program around 50ms later
    • ..but you don't expect them to be synchronized to anything, so you have probably never even noticed this delay.
    • yet if you are doing precise experiments and recording responses via the keyboard, being 50ms off is an amount you care about

Latency from physics

Some latency comes down to how you design devices to record it, and we'll be getting to that.


...but some is just already there to start with, due to physics.

It is helpful to know how much, in part because it places and upper limit to how much you can expect from the situation or a device.


Physics - sound latency

The speed of sound in 20°C air is 343 m/s.[1]

Or, seen from the perspective of time, it takes roughly 3 milliseconds to travel a meter.

So

talking to someone in the same room is easily (2m / 343m/s≈)5ms,
opposite ends of a 15m bus is around (15m / 343m/s≈)40ms,
halfway across a sports field is easily (35m / 343m/s≈)100ms.


(Aside: This speed is different in other gases, and in other materials. Speed of sound in water is roughly 1500 m/s. Speed of sound in the ground somewhat higher than that. Not generally things we have to deal with. See also [2])


With the way that our brain processes sound, delays under 50ms are not easily noticeable - at least not without a specific reference we know should match.

Say, if someone 40m away bounces a ball loudly enough for us to hear it, those ~120ms of difference might look a little off, or you might accept and dismiss it.

If you are both hearing them via the air and a (no-latency) walkie talkie, we now more distinctly hear the thing twice, and with enough time difference that it sounds like distinct things, even though we know it's not.


There are a few reasons this doesn't come up very often, including that distances where this becomes really noticeable are also distances sound through the air has already fallen off to negligible levels.

Exceptions to that are often loud things. Consider the estimation of the distance of a storm, by counting the time between lightning and thunder.

Physics - light latency

This article/section is a stub — some half-sorted notes, not necessarily checked, not necessarily correct. Feel free to ignore, or tell me about it.

Speed of light is approximately 300 000 000m/s.[3]

Or in maybe slightly more reasonable units, roughly 3.4 microseconds per kilometer.


For everyday people

  • human distances are crossed in human-negligible time
a typical room is crossed in a dozen nanoseconds
the scale of a city is crossed in dozens of microseconds.
  • human-noticable time imply large distances
if we would notice 50ms, light has traveled ~15000km in that time

These are timespans we don't really register, so this is not a speed we really register either.


At the scale of the Earth it already matters, though.

For example, if two shores on an ocean are 5000km apart, it would take light {{{1}}}16ms.

This is not relevant to us seeing it, as no one has vision or weather that good, and also there's the curve of the earth.


Yet it this starts putting some real-world numbers to, say, the delay over ocean internet cables.


Physics - electricity latency

You can assume the effect of electricity has roughly the same speed as light.

There are lots of footnotes to that statement, though.


Small tangent: While we may like to think of optical and copper as going at light speed, there are reasons it's slower

The speed of light in most fiber is actually a little under 70% of light speed, so a 5000km transatlantic internet cable would actually take light around 25ms to traverse (one way)(verify)
Electricity though copper or aluminium can go as fast as 99%, or as slow as 50%(verify), depending a little on what you're doing (this gets into some deeper electromagnetic details). But crudely: it's often comparable to fiber but much more of a pain to do over longer distances

Latency for everyday people

Latency for gamers

Latency for music producers

Latency for behavioural experiments

(Terms like 'behavioural experiments' and 'experimental psychology' has a few senses, we here primarily mean the one where we measure interactions with of a person. Whenever that involves reaction time, it draws in latency considerations)


In a behavioural experiments run on a computer, both the presentation of one or more stimuli, and the recording of a response, will all incur some amount of latency. And if you care about timing, then you care about the relative timing about all of those to all others.

Being scientists, we want to measure both delay and its variability in our measurements, and proably reduce it where we can, because

variability affects precision (largely about how consistent measurements are, the trial-to-trial variability)
and both delay and variability affect accuracy (how far measurements are from their true value).

Both precision and accuracy affect how much your data will be able to tell you about what actually happened.



More individual puzzle pieces

Device latency

Electronic latency

Inter-device latency: different devices aren't necessarily synced, nor communicate instantly

Compared to what?

Verifying

Communication latency in common-enough devices

PCIe latency

This article/section is a stub — some half-sorted notes, not necessarily checked, not necessarily correct. Feel free to ignore, or tell me about it.

tl;dr: assume order of dozens of microseconds, sometimes better sometimes worse.


Once it's going, the speed it can move bytes at is huge, so the time between data is miniscule.

But in one of the meanings of bandwidth is not latency, the overhead to setting up a transfer is due to various rules, and in many cases is non-negligible.


The overhead of a transfer starting up may be order of 0.2us to 2us.

The overhead from it being available in one device to it being available in another may be order of 20us in a good case, up to hundreds of microseconds if things are congested or has to take multiple hops (due to switching or transfer across multiple CPUs).


Footnotes:

  • PCIe tunneled over something (e.g. Thunderbolt, USB-C) will be more variable.
due to the way it's tunneled, this can actually be quite good, but no guarantee(verify)
  • a driver a driver and software always have the option to not try the hardest it possibly could, and may even have good reason for that (e.g. power draw).

USB latency

PC keyboard and mouse latency

PC graphics latency

PC monitor latency

Vsync and double buffering
Scan-out time
Rescaling

Serial port latency

Parallel port latency

Microcontroller latency

PC multitasking latency

Digital audio latency

Audio output latency

Audio input latency

How low can you go?

Wireless audio latency

DAW latency

"Latency compensation"