# The Algebra of Units

## Dimensional Analysis for Fun and Profit

The first few times I taught this class, I was surprised to find students turning in homework solutions with no notion of what units they were using --- seconds, cycles, instructions, programs, microseconds, kilograms, furlongs per fortnight, etc. I realized that there was no single course (except perhaps freshman Physics) in which students were taught about units.

## Prefixes

Students are often confused by all the "milli", "micro", "nano", "mega", "µ" prefixes they see. Here's a simple table defining them. A more humorous (although still correct) discussion is at the Free Online Dictionary of Computing.

## Fundamental Units

In computer evaluation, the important units are typically
• seconds
• instructions
• machine cycles
• programs
• dollars
All other units will be based on these: for example, if the price performance of a computer were measured in "MIPS per kilobuck", that would mean "million instructions per second per thousand dollars", i.e.
106 * instruction/(second * 103 * dollar)
or equivalently 103 * instructions / (seconds * dollars).

One particular unit that often confuses people is the Hertz (abbreviated "Hz"), a common unit of frequency. A Hertz is technically 1 divided by a second, but what people usually mean is 1 "cycle" per second, whatever a "cycle" is. For example, a radio station broadcasting at 93.9 MHz is producing a signal that oscillates 93,900,000 times per second, and a 300 MHz processor has a clock that pulses 300,000,000 times per second.

The electrical side of the course uses different units:

• seconds, a unit of time
• electrons
• Joules, a unit of energy (= 1 kg m2/sec2)
• Coulombs, a unit of charge (a certain, very large, number of electrons)
• Amperes, a unit of current (= 1 Coulomb/second)
• Volts, a unit of electrical potential (= 1 Joule/Coulomb)
• Ohms, a unit of resistance (= 1 Volt/Ampere)
• Watts, a unit of power (= 1 Joule/sec = 1 Volt * 1 Ampere)
Each of these, of course, can be modified by SI prefixes; the most common examples are "kilohms" (103 Ohms), "milliamps" (10-3 Amperes), and "µsec" (10-6 seconds).

## Choosing appropriate units: what's "good"?

When I lived in Canada, somebody told me the joke "Canada has the most efficient postal system in the world. Where else can you send a letter for only a penny a day?" [Canada Post at the time charged something like 40 cents to mail a first-class letter.]
Whether we're trying to send a letter arriving as soon as possible for as little money as possible, or buy a computer that gets the job done as soon as possible for as little money as possible, we need a sensible way to balance cost with time. Miles per gallon is a good way to measure a car's fuel efficiency, because we want to travel a lot of miles and burn as few gallons of gas as possible; doubling both means you've gone on a longer trip, but your efficiency rating should be the same. By contrast, cents per day is a bad way to measure a postal service's efficiency, because you'd really like to decrease both the cost and the delay, rather than being willing to increase one of them as long as the other increases too.

Let's say we want large values of our measurement to indicate a "good" computer, combining cost with speed. We choose a unit that increases as the amount of work (measured in, say, instructions) increases, so we'd better have instructions in the numerator; on the other hand, the more seconds the computer takes, the worse it is on this measure, so seconds had better be in the denominator. Dollars are likewise "bad", so we put them in the denominator too. Thus we're forced to something like "instructions / (dollars * seconds)". We might then try some real-world examples -- say, a computer that executes 3000000 instructions in 0.1 seconds and costs 4000 dollars, coming to 7500 instructions per dollar-second -- and conclude that these numbers were too large to handle conveniently, especially as computer performance improves in the future. So we would perhaps decide to scale the unit by a factor of a thousand, getting
7.5 * 103 instructions / (dollars * seconds)
which is in exactly the unit described above. (I called it "MIPS per kilobuck" not only to illustrate the prefixes, but also because people often use MIPS and kilobucks by themselves, so it's easy for people to understand dividing one by the other.)

## The Rules of Algebra

I did some algebraic manipulations in the previous paragraph that you may not understand. The first rule of thumb is to

### Treat each unit as though it were a different, unrelated variable

For example, 3x + 4x can be written more simply as 7x, but 3x + 4y can't be simplified unless x and y are related somehow. Similarly, it makes sense to add 3 instructions to 4 instructions, getting 7 instructions, but adding 3 instructions to 4 dollars gives you no new insight (and in fact is pretty meaningless).

This first rule of thumb gives rise to the more specific

### Don't add, subtract, or compare two quantities unless they are measured in exactly the same units

If they're in units that can be converted into one another, like seconds and hours, then convert one into the other before adding or subtracting: thus
2 hours + 15 seconds =
2 hours * (3600 seconds/hour) + 15 seconds =
7200 seconds + 15 seconds = 7215 seconds
Why am I justified in multiplying part of the equation by 3600 seconds/hour? Because 3600 seconds is one hour, so 3600 seconds divided by one hour equals 1, and I can multiply anything I want by 1. (I could legally have multiplied by 1 hour/3600 seconds, but then I would wind up with 1/1800 hour2/second, which while technically correct is still not addable to 15 seconds.)

If, on the other hand, they're in units that cannot be converted into one another, like seconds and instructions, then their sum and difference are meaningless and you're probably doing something wrong by even wanting to add or subtract them.

In a well-known Far Side cartoon, Albert Einstein is staring puzzledly at a blackboard on which are the equations "E=mc3", "E=mc4", etc. successively crossed off, while a cleaning lady clears his desk and says something like "That's better, got that all squared away. Yessirree, all squared away."
It's a good cartoon, but anybody who knows his units (as Einstein certainly did) couldn't possibly have believed "E=mc3" or "E=mc4" for a moment: the units on the two sides of the equals sign don't match.
Mass (the m in the equation) is measured in grams or kilograms. Speed (e.g. the speed of light, c) is measured in centimeters per second or meters per second. Energy is measured in joules or ergs, which are in turn defined in terms of mass, distance, and time units: in particular,
1 erg = 1 g * cm2 / (second2)
1 joule = 1 kg * m2 / (second2)
In other words, energy is measured in g*cm2/sec2, exactly the same units as mc2. But mc3 would have units of g*cm3/sec3, which isn't equal to any amount of energy for the same reason that a mile is not equal to any amount of time, money, or mass.
Which is not to say that the equation "E=mc2" is trivial. Saying that there is a fixed ratio between matter and energy was not obvious before Einstein, nor was saying that the ratio was the square of the speed of light. However, if there was to be such a ratio, it would have to be the square of some speed, just to make the units come out right.

### You can multiply or divide any units

If you drive 35 miles in half an hour, your speed can be described as
(35 miles) / (0.5 hours) = 70 miles/hour.
We all know that miles/hour is a reasonable unit; so is miles/gallon. What if you multiplied your speed by your gas mileage? The result would be measured in miles2 / (hour * gallon), and might be a very reasonable measure of the fuel efficiency of your car, taking into account your desire to get somewhere quickly. Similarly, the above example of MIPS per kilobuck is quite a plausible unit if you want to measure how much computing speed you're getting for your money.

### Decide what are reasonable units for the answer before you work it out

If a question in the textbook asks "How long does Machine X take to run Program Y?", the answer had better come out in seconds (or hours or milliseconds or something like that). If you work through all the arithmetic and algebra and come out with an answer in seconds per cycle, or dollars per instruction, you've done something wrong.

### Let units guide you in your algebra

If you've decided that the answer to your problem should come out in seconds, and you have at your disposal a number of instructions, a number of cycles per instruction, and a number of instructions per second, the only way it makes sense to combine them is to divide the number of instructions by the number of instructions per second:
(3.5 million instructions) / (5 million instructions/second)
= 0.7 seconds
after cancelling the instructions in numerator and denominator. Again, we're treating units essentially like variables. (Note that the number of cycles per instruction wasn't actually used in the calculation at all.)

If you guessed wrong, and multiplied the number of instructions by the number of instructions per second, you'd wind up with an answer measured in instructions2/second, not in seconds, so you'd know you had done something wrong.

## Comparisons

Many problems in the textbook ask you "How much faster is Machine X than Machine Y?" or questions like that. By the above rules, you can't answer this question until you've come up with some measure of "speed" for both machines, measured in the same units. But once you've got that (let's say, in MIPS, although that's a questionable way to measure speed), how do you compare the two? My students in the past have done one of two things:
• Subtract SpeedY from SpeedX, producing an answer like "Machine X is 4.5 MIPS faster than Machine Y". This is technically correct, but not particularly useful, especially when you say it to your boss who doesn't know a MIPS from his elbow.
(Actually, all too many students have started with the wrong units and wound up with answers like "Machine X is 8 seconds faster than Machine Y", which is utterly meaningless! This sort of comparison makes sense only if you're talking about a single task which one machine performs in 8 seconds less than the other one does. For a different task, or a larger or smaller version of the same task, it provides no useful information at all.)
• Divide SpeedX by SpeedY. Since both speeds are in exactly the same units, the units cancel and we're left with just a number like 1.3, interpreted as "Machine X is 1.3 times faster than Machine Y" or "Machine X is 30% faster than Machine Y". This sort of answer is really much more helpful: if your speed figures are truly representative of machine performance, it tells you that any task you choose to run will finish 30% faster on Machine X than on Machine Y.