20

Got asked in an interview. They asked to order the following in terms of speed:

  • CPU register access,
  • Context switch
  • Memory access
  • Disk seek.

pretty sure the disk seek is the slowest and register access is the fastest, but not quite sure about the two in between. Can anyone explain it a bit?

Charles
  • 50,943
  • 13
  • 104
  • 142
Wudong
  • 2,320
  • 2
  • 32
  • 46
  • A main memory access takes roughly 100 ns. Context switches occur roughly every 1,000,000 ns. Of course, not all the time is spent switching contexts, but it should be pretty easy to see we are taking about constructs on vastly different scales. – Andrew Tomazos Oct 20 '13 at 16:16

1 Answers1

25

I happened to find a surprisingly good answer on Yahoo!:

Fastest to slowest:

  1. CPU
  2. Memory
  3. Context switching
  4. Disk

Although:

Disk access may be significantly faster at times due to caching ... so can memory access (CPUs sometimes manage a caches from main memory to help speed up access and avoid competition for the bus).

Memory access could also be as slow or slightly slower than disk access at times, due to virtual memory page swapping.

Context switching needs to be extremely fast in general ... if it was slow then your CPU could begin to spend more time switching between processes than actually performing meaningful work when several processes are running concurrently.

Register access is nearly instantaneous.

(emphasis mine)

I agree with that answer.

th3an0maly
  • 3,360
  • 8
  • 33
  • 54
woz
  • 10,888
  • 3
  • 34
  • 64
  • 1
    On the other hand, a context switch probably involves several memory accesses. On average, those locations will be in the active set, but then, for the most part, that is also the case for your typical memory access. – tripleee Sep 04 '12 at 19:28
  • @tripleee I agree. If you compare the best case memory access and the worst case context switching, the order probably would be different. – woz Sep 04 '12 at 19:31
  • Well, even best case to best case. When you switch context, you need to recover registers, the stack pointer, and the instruction pointer, at a minimum, which translates to a number of memory accesses. – tripleee Sep 04 '12 at 19:38
  • I guess I interpreted context switching as a function of only the CPU and cache memory, which is very fast, whereas going out to your RAM takes much longer. However, if a context switch happens to involve a trip to your RAM, then "memory access" would be a part of "context switching", which would of course make context switching the slower of the two. – woz Sep 04 '12 at 20:04
  • 1
    @tripleee: not necessarily. If your hardware has shadow registers or multiple contexts on chip, it might not involve any actual accesses off chip. – Chris Dodd Sep 04 '12 at 20:38
  • 7
    I find it slightly amusing that a Google interview question can be answered by using Yahoo!. – Mark McDonald Oct 31 '12 at 05:17
  • Generally a main memory reference implies the memory is resident, and does not entail a page fault. Further a context switch happens only in the order of thousands per second, so they don't have to be that fast to not dominate a heavy switching environment. I think the correct answer is that a main memory access is much faster than a context switch. – Andrew Tomazos Oct 20 '13 at 16:20
  • @AndrewTomazos I don't understand your reasoning. I would think that you could do thousands of operations within the processor before you could do one round trip to the RAM. – woz Oct 20 '13 at 20:30
  • @woz: No, the fastest instruction, such as incrementing a register, takes about 1 ns. Reading from uncached main memory takes about 100 ns. A context switch has to do a whole bunch of stuff to reconfigure the core for a different process and takes in the order of 10,000 ns I would say. Recall context switches generally only occur thousands of times per second. Performance degrades due to context switching after this which is evidence of a context switches scale. A context switch on Linux for example entails an interrupt and then a long kernel function in the scheduler... – Andrew Tomazos Oct 20 '13 at 21:08
  • @woz: ... simply put, its just a totally different scale. – Andrew Tomazos Oct 20 '13 at 21:08
  • 2
    @woz: Heres some empirical evidence: http://blog.tsunanet.net/2010/11/how-long-does-it-take-to-make-context.html. As I state context switches are in the order of 10,000ns, about 100 times slower than a main memory access. – Andrew Tomazos Oct 20 '13 at 21:24
  • @ChrisDodd: can you name any specific commercial system which can perform a context switch faster than accessing main memory? – MikeB Jul 27 '14 at 15:56
  • 2
    @MikeB: ARM chips all have shadow registers for fast interrupt context switches. The Tera MTA had many registers sets and did a context switch every cycle; some GPUs work the same way. The SparcT4 has multiple register contexts and does a context switch when a memory access misses the cache. – Chris Dodd Jul 27 '14 at 18:41