r/askscience • • Jan 02 '14
Computing Why do computers have to load?
Theoretically, since the electrical signals inside your device travel at the speed of light, why do computers have to load programs?
(that may be completely wrong, and I suspect it is. So explain to me, please)
9
Upvotes
27
u/DanielSank Quantum Information | Electrical Circuits Jan 02 '14
A lot of people are sort of implying that the finite speed of light is an important part of why computer operations take noticeable amounts of time. This is incorrect.
The physical components that store information in a computer change from one state to another at a speed determined by circuit parameters like resistance and capacitance of the connecting wires and transistor gates. The speed of light has nothing to do with it. Suppose it takes 1 nanosecond for a transistor to go from ON to OFF. In that case the clock that triggers each operation in the CPU has to run at 1 GHz or slower [1]. A speed of 1 GHz means that the CPU does 1 billion operations per second. That is a finite speed, and as such it will take that CPU a finite amount of time to complete an operation. For example, a program that requires 1 billion operations to complete will take 1 second [1].
When booting the computer or starting a program the machine must first retrieve information from the hard disk. This is slow because the hard disk is a mechanical machine with a head that has to move around to different parts of the disk. That moving around makes up a significant fraction of the computer boot time, which is why solid state drives lead to much shorter boot times.
[1] I'm over-simplifying on purpose.
6
u/Niriel Jan 02 '14
The computer in your microwave is not fundamentally different from the one with which you are reading this. Processor, memory, inputs and output devices. Why is the microwave ready to operate as soon as it is plugged while a desktop computer can take a couple of minutes?
The microwave has a single program, coded into a read-only memory (ROM). As soon as the microwave is powered up, the processor starts executing the instructions in this chip. It needs nothing else than that program so it is good to go.
A general computer (and for this I'll even call iPhones general computers, despite the efforts of Apple) would be boring if it had only one program. So there is a ROM that contains a program, but that program ("firmware", like BIOS for instance) starts another program ("boot loader") which starts another program ("operating system" or OS), which goal is to let the user start the program they want ("application"). There can be even more layers. For example, the boot loader could load a second-stage boot loader (like GRUB), which lets you choose which OS you want to start (on a machine with several partitions and/or windows/linux versions).
But before the firmware even tries to localize the boot loader on your hard drive/CD/usb-stick, it has do do a lot of waking-up and setting-up of all the hardware. That is called "Power-on self-test", and even though most of it is on-chip (and not mechanical like hard drives) there is a lot to do and this takes time. A microwave does not need that since its hardware is fixed.
The first operating systems were small and did not offer much choice so they were straightforward and quick to load. Nowadays, many operating systems are very modular. You can choose your kernel, your file systems, your command-line interpreter, your display server, your compositing manager, your widget library, etc., all of that which is supposed to load before you can actually DO something. Each of these many pieces of code reside in many files, each with configurations and data that reside in many other files. This is a LOT of file access, and accessing many small files is slower than accessing a big one (at least with mechanical disks). However, slow mass storage is not the only source of slowness.
According to this article, processors became a thousand times faster in 20 years but the memory speed was not even multiplied by ten. This means that memory is a massive bottleneck. Moving data from RAM to CPU registers can take several hundreds of CPU cycles, that means that the CPU could have been executing 200 instructions but it cannot because it is waiting for that stupid data to come from memory. So we gave smaller and fasters tiny bits of memory to the CPU, called "cache", but this is not enough. Indeed many modern programming languages emphasize things like "objects" and "pointers", which means that the data is scattered in tiny chunks all over the RAM instead of being consolidated in continuous bytes of memory. As a result, programs written in these languages (and that is MANY) are slow because the CPU caches hardly ever contain the data you want. This is not a problem with the first steps of the boot process, such as boot loader, and even kernel, but it becomes a massive problem when the graphical user interface starts working.
TL;DR: not only the hardware is slow, but there are a LOT of things going on before the machine lets you do something.
1
u/fathan Memory Systems|Operating Systems Jan 02 '14
One important point that several people have hinted at but not said directly is that the memory your computer uses directly is volatile, meaning it's wiped clean when the power goes off. The non-volatile storage in your computer (the harddisk, basically) keeps things when the power goes off. When your computer turns on, it needs to take the data off the non-volatile storage and put it into the volatile storage where it can use it directly.
As others have said, non-volatile storage is slow (compared to volatile) and there's a lot of data to move, so this takes some time.
1
u/prickneck Jan 02 '14
The load time you experience is essentially the time it takes to load data from disk into memory (RAM) and then process this data once it's in memory.
Even with recent advances in hard disk technology (solid state drives and so on), the speed of loading data from hard disk is many times slower than accessing it in RAM and therefore the majority of "load time" is comprised of this process. It does, however, depend on the program, which might be loading a small amount of data and then performing millions of complex calculations before the user is presented with something they can interact with. In this case, the bottleneck is CPU speed, as opposed to the hard disk.
1
u/rocketsocks Jan 02 '14
There are two key parts to a computer, and they work in tandem. The first part is the processing hardware (the CPU), and it is basically instant on, it takes only the tiniest fraction of a second to go from unpowered to capable of any computation. The second is the "state" of the computer, and this is a complex extended system that requires a lot more steps to set up.
If all you want to do with a computer is add two numbers and store the result in a register, or something similarly low level, then a computer can do that in a single clock cycle and likely just as quickly after power is applied. However, we want computers to do a lot more for us. We want them to be connected to storage and displays. We want them to be able to communicate over the internet. We want them to run operating systems. And so on. Each of those activities requires a program. Those programs must be stored somewhere and in some, and accessing that storage also requires running programs. So there's a complicated bootstrapping dance that allow a computer to go from being turned off to having all of the relevant programs set up and running or available. Note that the term "booting" a computer comes from bootstrapping.
In cooking there is a concept called "mise en place", which is basically the organization of the ingredients and tools necessary to make a dish all set up so you have easy access to them when you start cooking. Booting up a computer is similar. All of the relevant code needed to do stuff is placed at hand for when it's needed.
What you think of as the "computer" is actually a rather significant amount of software running on top of the raw hardware. All of that software needs to be retrieved from non-volatile storage (such as a hard drive) loaded into RAM, various initialization routines need to run, background services need to be set up, and the main kernel needs to be running.
Consider the elaborate choreography that occurs when you type a single letter into the search field on google loaded in a browser. First, pressing the key fires off an event at the hardware level which causes a small sub-routine to run which stores a code denoting which key was pressed to be stored in a particular part of memory. Next, the pressing of the key causes code within the OS to run which keeps track of what applications are running and which component currently has focus at the moment and the OS passes off that information to the browser. The browser then figures out what to do with the signal, updates the state of the text input box so that the newly typed letter appears there, runs code to interpret javascript code that was tied to keypress events within that input field. There's an entire javascript engine running an interpreter/compiler which runs code which enables sending that keypress plus other data off to google. And this is achieved through yet more code which runs as part of the OS's network sub-system as well as the drivers for the network adapter, there is an elaborate, multi-layered dance of sub-systems that enables sending data to google and the return of instant search results and suggestions. This data is handed off from sub-system to sub-system until eventually it makes it to the javascript sub-system in the browser where it is processed and then used to update the currently displayed page. Which triggers the execution of various parts of the browser's code such as CSS processing, page rendering, additional javascript running, and so on. Meanwhile, all this time the OS has been running underneath everything, keeping track of windows and controls, constantly repainting the entire screen, and doing a million other background tasks and sub-systems. If you could visualize this you would see code execution jumping around across different parts of memory every fraction of a second.
We tend not to appreciate this sort of thing normally because usually it's just so very seamless and unobtrusive. But the fact is that your computer has to execute a tremendous amount of code just to process a keystroke and display the result on screen, and in order to do that it has to have a lot of code loaded into memory and properly configured, which takes a long time.
It is possible to store this state on disk and then retrieve it on boot and put it back in memory (this is called "hibernating") but even doing that requires a fairly extensive bootstrapping procedure (the system needs to be able to use various hardware, it needs to be able to read the data on the disk, etc.) which still can take more than a few seconds.
5
u/[deleted] Jan 02 '14
[removed] — view removed comment