LibraryQuant puzzles
Somebody installs Python on a laptop for the first time. Three things then happen.
Three questions, one per conclusion. Is each of them right, and if not, what actually happened?
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?
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.
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.
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.
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?
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.
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.
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