What is a context switch
Every time it switches jobs, the computer writes down how far it got, then reads that note back when it returns. Behind all that turn-taking is this little note.
Leave a note when you stop
Imagine you're working at your desk and suddenly have to do something else. If you just get up and leave, you won't remember where you were when you come back.
So you jot down a note saying "got this far" before you leave, and when you come back, you read it and pick up right where you stopped.
A computer does exactly the same. When it pauses a job, it saves the current state in full, like a note, and when it comes back, it loads that state just as it was.
When you pause a game and start it again later and it continues from the same spot, that's also because the state at the moment of pausing was saved.
Write down the state before leaving, then come back and carry on exactly where you were.
When a job stops, its state is saved; when it resumes, that state is restored.
Slowing down the moment of switching
Let's slow down that brief moment of switching jobs.
First, job A is running. When it's time to switch, A's state is saved, and then job B's saved state is loaded. Only then does B start running.
The important point is that during the save and load, neither A nor B can do any work. That time is spent purely on switching places.
Restoring can only begin after saving finishes, so this empty stretch can't be skipped.
Between saving and restoring, no job makes any progress.
A switch happens in two steps, saving and restoring, and nothing gets done in between.
The more often you switch, the more you lose
Switching always has a small cost: the time spent writing the note and reading it back.
Use the slider below to set how many times the job switches within the same 1 second. As the number of switches rises, the time spent switching grows, and the time spent actually working shrinks.
Switch too rarely, and one job hogs the seat for too long; switch too often, and you're too busy switching to get any work done. That's why a balance is needed.
When a computer slows down with too many programs open, one of the reasons is that switching time piles up.
Move the slider to see how much the switching cost eats into working time.
Switching isn't free, so switching too often eats into working time.
Same time, different results
Let's look at two approaches side by side over exactly the same time.
The left one switches only now and then. Few cells go to switching, so many cells go to work. The right one switches very often. A switching cost squeezes into every cell, so few cells actually get work done.
The same amount of time passed for both, but when it's over, they've handled different amounts. What made the difference wasn't the amount of work but the time thrown away on switching.
It's just like a person who keeps jumping between tasks: busy all the time, yet finishing very little.
Both rows are the same length, but the one that switches often has fewer working cells.
When switches are frequent, you get less done even in the same amount of time.
Summing it up on one page
There are just three things to remember.
First, the note is the state: saving all the progress so far when a job stops is what leaving a note means.
Second, switching is two actions, saving and loading back, and no job moves forward in between. Third, that's why switching too often is costly: as switching time piles up, the time left for real work shrinks.
In the end, switching is the secret to doing many jobs with a single worker, and the knack is to switch just the right amount.
The note is the state, a switch is saving and restoring, and switching too often costs you.