Timing software

When measuring how long it takes to execute a program, people often report the best time out of several runs with the same input. That seems odd at first. Why report the best time? Why not report the average time?

Operating systems have more to do than just run your program. The time that elapses between the start and finish of your program may vary because of other demands on the operating system’s resources. By taking the best time, you (presumably) get an idea how much time it takes to run the program itself with minimal overhead from other operating system activity.

The best-time approach is appropriate when comparing the efficiency of two programs: compare the best time for Program A to the best time of Program B. But if you want to estimate how long you can expect wait for Program A itself to run, not comparing the efficiency of Program A to anything else, then averaging the run times is reasonable.

(Everything above assumes constant input. This post is not about analyzing execution times with varying inputs. For example, you might use best-case timing to compare two sorting algorithms by sorting the exact same list several times. The only thing changing from run to run is the state of the operating system, not the input. Of course you might also want to measure the average run time with multiple randomly generated data sets, but that’s a different matter.)

Emacs command to add HTML tags

A while back I asked Jason Fruit how to add HTML tags to text in Emacs, something like the format painter in Microsoft applications. He said he didn’t know of anything and wrote the following macro for me.

(defun tag-word-or-region (tag)
    "Surround current word or region with a given tag."
    (interactive "sEnter tag (without <>): ")
    (let (pos1 pos2 bds start-tag end-tag)
        (setq start-tag (concat "<" tag ">"))
        (setq end-tag (concat "</" tag ">"))
        (if (and transient-mark-mode mark-active)
                (goto-char (region-end))
                (insert end-tag)
                (goto-char (region-beginning))
                (insert start-tag))
                (setq bds (bounds-of-thing-at-point 'symbol))
                (goto-char (cdr bds))
                (insert end-tag)
                 (goto-char (car bds))
                 (insert start-tag)))))

I added the following line to my .emacs file to bind Jason’s macro to the key sequence C-x t.

(global-set-key "C-xt" 'tag-word-or-region)

To add a tag to a single word, place the cursor before or in the word and execute the command. To tag a block of text, select the text first then execute the command.

Related posts:

The Stone Age didn’t end because we ran out of stones

According to Richard Sears, the world hit peak oil in 1985 in the sense that oil accounted for 50% of world energy in 1985 and the percentage has been declining since then. By that same measure, we hit peak coal in the 1920’s and peak wood in the 1820’s. Sears summarizes

For 200 years we have been systematically de-carbonizing our energy system.

We didn’t stop using wood as our primary energy source because we ran out of trees; we moved on to something better better. Sears believes we are now in the process of transitioning from oil to renewable energy sources, and not because we’re running out of oil. He concludes his presentation by saying

… the Stone Age ended, not because we ran out of stones. It’s ideas, it’s innovation, it’s technology that will end the Age of Oil before we run out of oil.

A tale of two espresso machines

This post tells the story of two espresso machines: one in Los Angeles and one in Brenham, Texas. But it’s more about deciding what you do and do not want to control.

* * *

In his book Made by Hand, Mark Frauenfelder chronicles his quest to make great espresso at his home in Los Angeles. He reviews some of the tricks to make good espresso from a relatively inexpensive espresso machine. (The machine he describes, a Rancilio Silvia, was around $500 when Frauenfelder bought it. That’s a lot more than a Mr. Coffee, but it’s cheap compared to espresso machines that cost over $10,000.) The problem with inexpensive espresso machines is that they have poor temperature. The water temperature can vary as much as 40 °F. Frauenfelder hacked his espresso machine by replacing its controller with a more sophisticated proportional-integral-derivative controller or PID.

(Small changes in brewing temperature can have a large impact on coffee taste. This is because coffee is chemically complex. Brewing at different temperatures extracts these chemicals in different proportions. See this Scientific American article for details.)

The context of Frauenfelder’s story is his book on doing things yourself, not to save money, but to have more control of your environment. He describes his adventures from growing vegetables to making musical instruments for the joy of doing so.

* * *

Roger Sessions tells a very different story about why he does not have his own espresso machine in The ObjectWatch Newsletter. He describes why he drives 10 miles every day to the Starbucks in Brenham. He says he saves money, even though he spends more on gasoline than the price of his doppio macchiato. For starters, Sessions estimates it would take nearly four years to pay off his hardware investment. Then he lists the things that could go wrong:

  • Something might break down.
  • Something could short out his electrical system.
  • The roaster could burn his house down.
  • He might not be able to use the equipment well.

The context of Sessions’ story is the benefits of software as a service. Even when it appears to be more economical to create and host your own software, you may save money by paying someone else to offer that software as a service. Sessions pays Starbucks to make his espresso for him because they not only make the capital investment in hardware, they’re also responsible for operation and maintenance. (Update: See Roger Sessions’ comment below.)

* * *

Sessions makes as strong a case for not owning an espresso machine as Frauenfelder makes for owning one. Frauenfelder speaks of the confidence and joy that comes from having detailed knowledge and control of the things around you. Sessions speaks of the hassle and expense of being responsible for the things around you. Both have valid points. I’m sure there are areas in which Frauenfelder is happy for someone else to take responsibility, and areas in which Sessions enjoys fine-grained control. But they disagree which approach is preferable when it comes to making espresso.

One of the things that prompted me to buy Mark Frauenfelder’s book was an interview I heard on a podcast. He said that he was attracted to do-it-yourself projects after becoming editor of Make Magazine. The spirit of the writers rubbed off. At one point in the interview he says DIY is not about saving money; in fact, the DIY approach will often cost more money. I appreciated this comment since many DIY enthusiasts justify their projects with dubious financial arguments rather than simply say that they enjoy what they’re doing and that the extra expense is worth it to them. I suspect Frauenfelder may agree with Sessions on the economics of espresso making, though they have different perspectives of the non-monetary benefits.  And although Sessions only argues monetary benefits, he also has non-monetary benefits to visiting Starbucks. I imagine he enjoys getting out of his house, seeing familiar faces, etc.

* * *

Posts quoting Mark Frauenfelder:

Endless preparation
Getting women to smoke

Post quoting Roger Sessions:

Three quotes on software development