Context Switching

Anshika Bhargava
3 min readOct 9, 2022

For Computers and Humans

Source: https://pixabay.com/illustrations/brain-turn-on-training-read-a-book-605603/

Humans and computers are similar in innumerous ways, one of them being context switching. So, let’s start with the basics.

What is context switching ?

Context Switching happens when you have to switch contexts between stuff.

Not very impressed with the definition, right ? Bear with me.

You must already be knowing what context switching means in terms of computing.

Operating systems perform context switching frequently. There can be many cases in which this happens.

  • It might happen that a process was running, but it required some I/O. Since the running process is now blocked, the operating system switched to another task to utilize the CPU.
  • Another example can be preempting of a lower priority task by a higher one. In this case as well, the operating system would have to do a context switch.

When context switching happens, the state of the previously running process needs to be stored so that it can be resumed at a later point, and the state of the process which is now scheduled needs to be fetched. This incurs a lot of overhead.

Something similar happens with our brain as well.

Let me humanize context switching.

Imagine you decided to solve a coding question. You read the question and started thinking about it. You are in the zone now, and about to finally crack the algorithm. You think you almost got it, and you are about to code it. Just then, your mother calls you and asks you to urgently pay the electricity bill, like right now. And since her wish is your command, you leave your question, and do that. And after that, you continue with your question again. Your brain, now, might take some time to start where you had left off. You will then eventually get back to your previous state and solve the question.

What you did here, my friend, is context switching.

Context switching is not uncommon in our everyday life. In the example above, context switching was easier as the task which our mother asked was menial and did not occupy much space in your brain. But the context switching time amplifies immensely when you are working on two different tasks at work and you have to constantly context switch between them.

In fact, this is an enormous problem for working professionals. A Loom article states that:

Lost productivity due to context switching costs the global economy an estimated $450 billion annually. That’s more than the GDP of most countries!

Research also shows that

Developers switched tasks on average 13 times an hour and spent just about 6 minutes on a task before switching to another one. An example of a task switch is a developer who is switching from implementing a feature to answering e-mails that are unrelated to the previous task.

This context switching can be anything from swapping tasks, answering a teammates’ questions, reviewing code of other tasks, attending meetings etc. While all these tasks are productive individually, too much context switching between these can often render a day less productive than what it could be.

Dealing with Context Switching

There is no way to completely get rid of context switching, especially in the workplace. Truth be told, we are expected to be able to switch context in an efficient manner.

One mechanism that I found pretty useful is figuring out the time when you achieve maximum productivity in the day. This can differ from person to person. The real challenge is to identify that time, and isolate yourself to focus on the most stimulating task at hand. The tasks that require most context switch can be left for the time when you already know you are not likely to be at your best.

If you felt the components of this article could have been aligned better, probably I was context switching while writing it 😛

--

--

Anshika Bhargava

Software Engineer at Google | I try to learn and blog