LibraryQuant puzzles

An Install With No Icon and No Window

Quant puzzle · intro · free, full solution

The question

Somebody installs Python on a laptop for the first time. Three things then happen.

  1. The installer says it finished. No new icon appears anywhere, and double-clicking nothing opens nothing, because there is nothing to double-click. The conclusion drawn is that the install failed and should be run again.
  2. They open a Terminal, which is the window where instructions are typed to the machine as text, and type a command with a letter out of place. Red text comes back saying the command was not found. The conclusion drawn is that something on the machine is now broken and the install should be undone first.
  3. A week later, a working script adds up a list of 2,000,000 numbers. Written as a plain loop it takes about 24 seconds. Handed to a library in a single call, on the same machine and the same numbers, it takes about 0.4 seconds. The conclusion drawn is that the laptop is too slow and a faster one is needed.

Three questions, one per conclusion. Is each of them right, and if not, what actually happened?

Hint

Think about how what was installed is normally used: by clicking on something, or by handing it a file to run? For the slow loop, compare the two runs: what stayed the same between them?

The idea behind it

Installing a language puts an interpreter on the machine, not an application, so the absence of a window is the expected outcome rather than a failed install. The same confusion, one step later, reads a refused command as breakage and a slow loop as a slow computer.

Worked solution

What the installer put there

The install did not fail. It did exactly what it says it does, and what it does is not what an install usually does.

Most installs put an application on the machine: a thing with a window, a menu and an icon, which a person looks at and clicks. Installing a language puts something else there. It puts a program whose whole job is to read a file of instructions, line by line, and carry each one out. A program of that kind is something called an Interpreter, and it has nothing to show a person, because its user is a file rather than a pair of eyes.

So the absence of an icon is the expected outcome. It is what a correct install of this kind looks like, and running the installer a second time produces a second correct install with no icon either.

Two ways to see that it worked, both of which happen in a terminal rather than on the desktop. Asking the program to report its own version, which prints a number and exits. Or handing it a one-line file that prints a word, and watching the word appear. Either of those is a success signal. Looking for an icon is not a test of anything, because there was never going to be one.

What the typo did

Nothing. It did nothing at all.

A command that is not found is a reply, not an event. The machine read what was typed, looked for something with that name, did not find one, and said so. Nothing was written, nothing was deleted, nothing was configured, and the machine is in exactly the state it was in before the return key went down.

This is worth saying flatly because it is where a great many beginners stop. Red text looks like an alarm, and the honest reading is closer to a shop assistant saying they do not stock that. The recovery is to type it again with the letter in the right place, and there is nothing to undo first.

The distinction to keep is between a command that refused and a command that ran. A command that ran can change things and can need undoing. A command that never started cannot have changed anything, and almost all of the red text a beginner meets in the first week is the second kind.

What the sixty-fold gap measures

Twenty four seconds against 0.4 seconds is a factor of about 60, on one machine, on one list of numbers. A second laptop is not going to close that, because the gap is not about the laptop.

The plain loop asks the interpreter to handle every number separately. For each of the 2,000,000 entries it works out afresh what kind of thing it is holding, what adding means for that kind of thing, and where to put the result. Two million separate decisions, each one small, and two million small decisions add up to 24 seconds.

The single call hands the whole list over at once to code that was compiled in advance, which already knows every entry is the same kind of number and never asks again. The decisions happen once instead of two million times, and the addition itself, which is the part anybody would have said was the work, was never the expensive part at all.

That is the shape of nearly every speed problem in this kind of work. The machine is rarely the answer. What changes the time is how many times the interpreter is asked to decide something, and the way to make it fewer is to hand over more per call.

Buying a laptop twice as fast would take the 24 seconds to about 12 and leave the 0.4 at about 0.2. The 60-fold gap would still be sitting there. If the machine really were the cause, how did the same machine manage the fast one?

One check

After an install of this kind, asking the program for its own version prints a version number in a terminal and no window opens anywhere. If a number came back, the install worked. If the command was not found, the usual cause is that the terminal was already open before the install finished and does not yet know the new program exists, and closing that terminal and opening a fresh one is the thing to try before reinstalling anything.

In short

Nothing failed. The installer put a program that reads instruction files on the machine, and a program of that kind has no icon and opens no window. The mistyped command left the machine exactly as it was, because an error message is a reply rather than damage. And the sixty-fold gap measures the interpreter making a fresh decision about every one of the 2,000,000 numbers, which is not something a faster machine would remove.

Learn quant finance on Thuztra Learn

Thuztra Learn teaches the maths of quant finance from zero, in short lessons, for readers who are unsure of it. Three questions in the app build your quant roadmap, free. Membership is a paid subscription. See what Thuztra Learn is and what it costs, or see the whole Core quant curriculum.

Get the app and answer the three questions:

Download on the Microsoft Store Download for Android

On Android, the app is a direct download, not a Google Play listing. Which devices, and what each one needs.

Further reading

Topics

Also in the library