Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

context switch vs memory access, which is faster? [closed]

Tags:

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?

like image 381
Wudong Avatar asked Sep 04 '12 12:09

Wudong


People also ask

Which context switch is faster?

A fast context switch is performed, whenever a functional unit comes across an operation destined for another unit. Switching contexts on each load/store instruction sequence allows a much faster context switch in the execution unit than previously published designs do.

What is the main disadvantage of context switching?

The disadvantage of context switching is that it requires some time for context switching i.e. the context switching time. Time is required to save the context of one process that is in the running state and then getting the context of another process that is about to come in the running state.

Why is context switching between threads faster than processes?

Context switches between threads are faster than between processes. That is, it's quicker for the OS to stop one thread and start running another than do the same with two processes. A context switch between processes is heavy.

How long do context switches take?

Each context switch takes the kernel about 5 μs (on average) to process. However, the resulting Cache misses add additional execution time that is difficult to quantify. The more frequent the context switches, the more your CPU utilization degrades.


1 Answers

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.

like image 84
woz Avatar answered Oct 06 '22 15:10

woz