Thursday, January 1, 2009

Different levels of storage (an analogy)

Computers have various layers of storage, since there's a well-understood tradeoff between speed and capacity. This post is an attempt to explain these levels based on "distance" - specifically, how far away they would be if information traveled at the speed of light. The speed of light doesn't have any special significance here, it's just a measure which puts things on a convenient scale. All measurements are made using my current CPU, an Athlon 64 at 2.4 GHz.

The first layer of storage is the register file. The register file is made up of a small number of individual registers, each of which holds one value. The access time is one cycle, or about 0.42 nanoseconds. In this amount of time, light travels about 12.5 centimeters, which is about the width of your hand.

The next layer is the L1 (level one) cache. L1 caches are generally pretty small (a few dozen kilobytes) and fast (a few cycles access time). They're also frequently split into separate caches for instructions and data, for reasons that I'm not entirely sure of. I was unable to find actual numbers for the L1 cache latency of my processor, but 3 cycles is a reasonable assumption, since it doesn't vary too much between processors. In the amount of time it takes to access the L1 cache, light travels about 37.5 cm.

The L2 (level two) cache is larger and slower - sizes are usually around a few hundred KB (512 KB in my case), and latency is higher as well. The only source I could find for this says that latency varies within this processor family, so I'm assuming 16 cycles latency, which is about at the middle of the range they give. At this speed, light travels about 2 meters in the time it takes to read from L2 cache. (Some processors also include an L3 cache, which is generally a few megabytes. I couldn't find any numbers on the access time, though, so I'm leaving it out.)

Everything before this point is built into the CPU itself. The next level is RAM, which is on separate chips. RAM capacities are generally in the range of a few hundred megabytes to a few gigabytes, and the access times are usually measured in hundreds of cycles. Assuming 500 cycles total latency for a random read, access time is about 0.2 microseconds, and in this amount of time light travels 62.5 meters, or about 200 feet. (NB: 500 cycles is a reasonable assumption, but I actually just made it up. >_> If anybody can find some more accurate numbers for random RAM access time, I'll update this post.)

Everything above this point is volatile memory - temporary space the computer uses for storage, which is lost when the computer is turned off. Flash-based SSDs are the first storage method in this list which is non-volatile, and can be used for persistent storage. Flash memory is relatively new, and despite being 10-100x more expensive than hard drives, is still a compelling alternative, for reasons you'll see in a second. The average SSD can store a few dozen to a few hundred gigabytes. A reasonable access time for a random read of flash memory is 10 microseconds. In this amount of time, light travels about 3 kilometers, or 1.86 miles.

Hard drives are radically different from everything discussed so far, since they have moving parts. Rather than having direct access to all the data at all times, you have a moving arm and a spinning disk, and the arm can only read the data which is directly underneath it. Hard drives are somewhat more difficult to calculate access time for, because the access time varies based on how far the disk has to spin, and how far the arm has to move, in order to read the data. I'm going to gloss over a lot of stuff about rotational speeds and seek times, and just tell you that 5ms is a reasonable access time for a hard drive. In this amount of time, light travels about 1500 kilometers, or 932 miles. (To put this in perspective, Texas is only 790 miles across.)

The Internet is often considered another level of storage, at least by some people. A quick test from my home computer, using ping, shows that latency to different websites is generally from 100-200 ms. Using this range, we can say that in terms of the speed of light, the Internet is about 30,000-60,000 km away, or about 2-5 times the diameter of the Earth.


Æther said...

Thanks p-static, I really liked this post since it puts things into perspective that I really haven't thought about. This reminds me, I've been meaning to get a flash drive better than my current one. (I think it has a capacity of 212 MB.)

Frank Church said...

Yeah, that's a neat entry. I've never thought about memory storage in computers much before, but I guess now I have.

Randomly: I've always loved that light travels about 1 foot in a nanosecond. Take that, metric system!

AEther: I got an 8 gb flash drive for Christmas, which is quite a bit larger than my old one. (Dunno how large that one is, but it can fit 7 audio CD's worth of mp3 files, so it's not big.)