Unrolling the Loops - Computerphile

**Understanding Postscript and Creating Computer File Banners**

When it comes to printing out computer file banners, having the right font and layout is crucial. A common choice for this purpose is the lcra font, which can be used at a point size of 30. To begin creating our postscript program, we define the variable n to be six, representing the number of computer files we want to print.

**Starting the Postscript Program**

Every postscript program must start with a percent exclamation mark, indicating that we are now working in postscript from this point forward. The comment "okay so how do we do it just using a postscript for loop" is followed by a brief explanation of how postscript works. It's worth noting that postscript does everything backwards in reverse polish notation, but in practice, it's a relatively simple language to work with.

**Using the Postscript For Loop**

The next line of code is "for" starting at 1 and incrementing by 1 up to n (which is 6), doing the following code inside the curly braces. This for loop allows us to iterate over each computer file we want to print. Inside the loop, we use the postscript command "select font," which specifies the font we want to use and its point size. In this case, we're using the lcra font at 30 points.

**Printing Computer Files**

The next line of code is where the magic happens. We want six computer files printed out, each with a height of 10 points from the margin, and spaced apart by 100 points. Looking carefully at the code, you can see that we're actually printing these files upwards, not downwards. This is due to the fact that our paper size is A4, which has a width of 8.5 inches but a height of only 11.7 inches.

**Using Abbreviations and Defining Operators**

As the postscript program continues, it becomes clear that using abbreviations and defining operators can be a powerful way to simplify code. For example, we define the letter "s" to represent the operator "show," and the letter "m" to represent the operator "move to." We also use these abbreviations throughout our program to make it more concise and efficient.

**Binding Definitions**

The postscript interpreter allows us to bind definitions that can't be changed later. In this case, we're binding the definition of "s" (show) to the original postscript command, and defining "m" (move to) to also represent the original postscript command. This ensures that our code is consistent and doesn't rely on any external changes.

**Transforming the Postscript Code**

One of the benefits of using postscript is its ability to transform text into images. In this case, we're using a transform program to create an image of six computer files being printed out. The resulting image is then displayed on the screen or outputted to a printer.

**Unrolling Loops and Replicating Code**

Finally, it's worth noting that if we were to unroll the loops in our code and replicate the same operations multiple times, our file sizes would increase significantly. This realization led to the development of pdf (Portable Document Format), which uses abbreviations and other optimizations to keep files small and efficient.

**Specifying Postscript for Interpreters**

Some postscript interpreters may require us to explicitly tell them when we're done with a file. In this case, we've used the "show page" command to specify that our program is complete. However, some interpreters may use abbreviations or transformations to create an image of the output file.

**Using Transform Programs**

Transform programs are a powerful tool in postscript. In this case, we're using a transform program to take our postscript code and convert it into an image. This allows us to easily display our output on the screen or printer.

**Postscript and Abbreviations**

The use of abbreviations is a key aspect of postscript programming. By defining short names for common commands and operators, we can make our code more concise and efficient. In this case, we've defined "s" to represent "show," and "m" to represent "move to."

**Binding Definitions and Abbreviations**

The binding process is an important part of postscript programming. By binding definitions that can't be changed later, we ensure that our code remains consistent and reliable. In this case, we've bound the definition of "s" (show) to the original postscript command.

**Postscript and Graphics Cards**

Finally, it's worth noting that some computers have powerful graphics cards that make them well-suited for postscript programming. The article mentions a computer with four Titan X graphics cards, which is one of the most powerful graphics card available today.

"WEBVTTKind: captionsLanguage: enback in the early 1980s john warnock the founder of adobe had invented postscript he'd pioneered all their work of getting a postscript interpreter embedded into laser printers he persuaded steve jobs at the time to say when your new macintosh comes out we want it to be almost a dual announcement at the same time but they didn't quite make it the macintosh did come out first of all a little nine-inch one which we've done videos on but alongside it fairly shortly afterwards was the apple laser printer john had got the killer app he thought to show off the skills and wonder of his laser printer driving with postscript the output and that killer example was a united states income tax form for supplementary income so he programmed this up in his postscript language you sent it down to the apple laserwriter which was so fast you couldn't believe it but it took a minute and a quarter to image it and the wondrous jobs of course said john i know how to present but i cannot hold them for one minute and a quarter while we press the button and we wait for it to come out and the light of the finish is going blink blink blink so john being a very good computer scientist and knowing all about his own language postscript said yeah what we've got to do steve is remove all the procedures just get them automatically removed and unroll the loops and make sure that every if statement that we know what data goes into that if statement so we know which way it's going to go through the program so we're going to flatten the program as it's sometimes called we're going to linearize it we're going to bind in the data and say we want the fastest route to the final visual effect so that's what he did and the unrolled version imaged in 20 seconds not a minute and a quarter so it really did work we teach our students very carefully good programming principles we teach them to use if statements for loops blocks structured programming oh and above all else um isn't it nice to hive stuff off into procedures or subroutines whatever you want to call them and to call them up with super bowl arguments and it's clean and nice and structured and gets you very good grades in your coursework under the hood you always have to remember that there is a bit of a penalty performance wise to be paid for doing all this if you just consider the humble for loop for example every time you go round that loop the compiler is building in a checking mechanism at the top saying you said start at one and in steps of one go on until you get to six and do it around the loop like that and every time you go in that loop it has to say what's my loop counter at the moment oh i'm on three now let me take a look at how far i'm supposed to oh six is oh it's three less than six yes it is oh i think we'd better go around again it's that i exaggerate but you know what i'm getting at equally if you dive into a procedure you call it up with arguments do remember from what we've done already that that causes stack frames to be set up to propel your arguments to that function and to post them into the function and then when the function's finished doing its job it has to clear down that stack frame and if the function's recursive there might be zillions of stack frames to be cleared out it's all overhead but it's overhead that you're probably prepared to pay because it helps you think about the problem and do it neatly and elegantly but if you're concerned with performance of your program to the nth degree these overheads can begin to tell and sometimes to get the optimum and maximum performance under specialist conditions you might want to say rather than going around this for loop six times and doing pretty well exactly the same thing i will write it out by hand six times and forget the forward loop would that be quicker the answer is very often yes what i thought i'd do in the true spirit of computer file this little example that we'll do now we want to print out six computer file banners look at that correct font and everything as you all know it's called ocra i'm afraid that my uh printer is a bit deficient it's knocked off the corner of the pointy bracket but please forgive okay so how do we do it just using a postscript for loop well very briefly i'll go through this with you more details available on the specialist postscript video if you need it every postscript program has to start with percent exclamation mark this says i am postscript from now on comments in biscuit programs begin with percent in column one so program physics computer file banners define the variable n to be six here is the postscript command called select font which says what font you want and what point size do you want to do it at we're saying find the font called lcra and at point size 30 make me a copy of that ready for use in postscript here postscript does everything backwards in reverse polish notation but effectively it is very simple it's a for loop so it's basically saying four starting at one and in steps of 1 up to n but we know n is 6 do the following code inside these curly braces now the next line is saying we want 6 computer files and there should all be at 10 points in from the margin and they should be a hundred points apart when you look at this carefully i'm doing them upwards not downwards now you can see what happens when you start printing out stuff probably designed for us letter onto a4 paper your 10 point almost in what is it 72 points an inch i tried to do a 1 7 of an inch indent and it didn't work it's all to do with paper sizes but anyway going up the page at 100 point increments which is just a bit more than an inch 16 instances of computer file all beautifully printed out and there's the end of your for loop and finally it has as it were virtually imaged this for you inside its own graphic memory if you want it to show up on your screen or ultimately an old piece of paper you say show page now some post script interpreters if it gets to end the file you haven't said show page will put a show page in for you there's the show page here's the thing done not in a for loop but writing out six separate calls of the command to print out computer file at 30 point but you might say well you used things like show and move to and show page and here on this copy you're not no i'm trying to show you something which will be of use to us later on that postscript enables you to redefine operators and to abbreviate their names if you want to so up at the top here i say look define just the letter s to be the operator show define m to be the same as move to now the word bind just means remember i've done this definition and don't permit any other redefinitions that might mess things up don't let anybody else or don't let even yourself making mistakes try and change it again this is binding solidly m to being moved to select font have abbreviated as sf show pages sp and look the very first one is there that's 10 200 move to i'm there and then another then another and finally sp which is the abbreviation for show page but i didn't write it out by hand i got a transform program to do it for me because it's all right doing six of these with a bit of fancy abbreviation definitions at the top but if it was 10 000 times out i don't want to replicate something 10 000 in an editor or by hand here's a transform script which we'll leave there for in the notes if you get it working write us a comment will you and say yeah wow it worked for me you should better work out how to use it and i should say this is a much much much simplified version of the unrolled transform script that john warnock used back in 1984 to get his income tax form which was far more complicated than six computer files to print out in a reasonable amount of time on the apple laser writer just one final comment what john did and what this is echoing here in this abbreviation so why are you bothering to abbreviate the answer is well if you unroll the loops and replicate things you're making files bigger and bigger and bigger lots more printing isn't there in the fire and that's not necessarily a good thing can we keep file size down yes by doing abbreviations and that realization made by john in 1984 was the very earliest glimmerings of what we now know as pdf this particular server has four titan x graphics cards in it a titan x is one of the foremost graphics cards there are new generation 10 nvidia graphics cards coming out and some amd cardsback in the early 1980s john warnock the founder of adobe had invented postscript he'd pioneered all their work of getting a postscript interpreter embedded into laser printers he persuaded steve jobs at the time to say when your new macintosh comes out we want it to be almost a dual announcement at the same time but they didn't quite make it the macintosh did come out first of all a little nine-inch one which we've done videos on but alongside it fairly shortly afterwards was the apple laser printer john had got the killer app he thought to show off the skills and wonder of his laser printer driving with postscript the output and that killer example was a united states income tax form for supplementary income so he programmed this up in his postscript language you sent it down to the apple laserwriter which was so fast you couldn't believe it but it took a minute and a quarter to image it and the wondrous jobs of course said john i know how to present but i cannot hold them for one minute and a quarter while we press the button and we wait for it to come out and the light of the finish is going blink blink blink so john being a very good computer scientist and knowing all about his own language postscript said yeah what we've got to do steve is remove all the procedures just get them automatically removed and unroll the loops and make sure that every if statement that we know what data goes into that if statement so we know which way it's going to go through the program so we're going to flatten the program as it's sometimes called we're going to linearize it we're going to bind in the data and say we want the fastest route to the final visual effect so that's what he did and the unrolled version imaged in 20 seconds not a minute and a quarter so it really did work we teach our students very carefully good programming principles we teach them to use if statements for loops blocks structured programming oh and above all else um isn't it nice to hive stuff off into procedures or subroutines whatever you want to call them and to call them up with super bowl arguments and it's clean and nice and structured and gets you very good grades in your coursework under the hood you always have to remember that there is a bit of a penalty performance wise to be paid for doing all this if you just consider the humble for loop for example every time you go round that loop the compiler is building in a checking mechanism at the top saying you said start at one and in steps of one go on until you get to six and do it around the loop like that and every time you go in that loop it has to say what's my loop counter at the moment oh i'm on three now let me take a look at how far i'm supposed to oh six is oh it's three less than six yes it is oh i think we'd better go around again it's that i exaggerate but you know what i'm getting at equally if you dive into a procedure you call it up with arguments do remember from what we've done already that that causes stack frames to be set up to propel your arguments to that function and to post them into the function and then when the function's finished doing its job it has to clear down that stack frame and if the function's recursive there might be zillions of stack frames to be cleared out it's all overhead but it's overhead that you're probably prepared to pay because it helps you think about the problem and do it neatly and elegantly but if you're concerned with performance of your program to the nth degree these overheads can begin to tell and sometimes to get the optimum and maximum performance under specialist conditions you might want to say rather than going around this for loop six times and doing pretty well exactly the same thing i will write it out by hand six times and forget the forward loop would that be quicker the answer is very often yes what i thought i'd do in the true spirit of computer file this little example that we'll do now we want to print out six computer file banners look at that correct font and everything as you all know it's called ocra i'm afraid that my uh printer is a bit deficient it's knocked off the corner of the pointy bracket but please forgive okay so how do we do it just using a postscript for loop well very briefly i'll go through this with you more details available on the specialist postscript video if you need it every postscript program has to start with percent exclamation mark this says i am postscript from now on comments in biscuit programs begin with percent in column one so program physics computer file banners define the variable n to be six here is the postscript command called select font which says what font you want and what point size do you want to do it at we're saying find the font called lcra and at point size 30 make me a copy of that ready for use in postscript here postscript does everything backwards in reverse polish notation but effectively it is very simple it's a for loop so it's basically saying four starting at one and in steps of 1 up to n but we know n is 6 do the following code inside these curly braces now the next line is saying we want 6 computer files and there should all be at 10 points in from the margin and they should be a hundred points apart when you look at this carefully i'm doing them upwards not downwards now you can see what happens when you start printing out stuff probably designed for us letter onto a4 paper your 10 point almost in what is it 72 points an inch i tried to do a 1 7 of an inch indent and it didn't work it's all to do with paper sizes but anyway going up the page at 100 point increments which is just a bit more than an inch 16 instances of computer file all beautifully printed out and there's the end of your for loop and finally it has as it were virtually imaged this for you inside its own graphic memory if you want it to show up on your screen or ultimately an old piece of paper you say show page now some post script interpreters if it gets to end the file you haven't said show page will put a show page in for you there's the show page here's the thing done not in a for loop but writing out six separate calls of the command to print out computer file at 30 point but you might say well you used things like show and move to and show page and here on this copy you're not no i'm trying to show you something which will be of use to us later on that postscript enables you to redefine operators and to abbreviate their names if you want to so up at the top here i say look define just the letter s to be the operator show define m to be the same as move to now the word bind just means remember i've done this definition and don't permit any other redefinitions that might mess things up don't let anybody else or don't let even yourself making mistakes try and change it again this is binding solidly m to being moved to select font have abbreviated as sf show pages sp and look the very first one is there that's 10 200 move to i'm there and then another then another and finally sp which is the abbreviation for show page but i didn't write it out by hand i got a transform program to do it for me because it's all right doing six of these with a bit of fancy abbreviation definitions at the top but if it was 10 000 times out i don't want to replicate something 10 000 in an editor or by hand here's a transform script which we'll leave there for in the notes if you get it working write us a comment will you and say yeah wow it worked for me you should better work out how to use it and i should say this is a much much much simplified version of the unrolled transform script that john warnock used back in 1984 to get his income tax form which was far more complicated than six computer files to print out in a reasonable amount of time on the apple laser writer just one final comment what john did and what this is echoing here in this abbreviation so why are you bothering to abbreviate the answer is well if you unroll the loops and replicate things you're making files bigger and bigger and bigger lots more printing isn't there in the fire and that's not necessarily a good thing can we keep file size down yes by doing abbreviations and that realization made by john in 1984 was the very earliest glimmerings of what we now know as pdf this particular server has four titan x graphics cards in it a titan x is one of the foremost graphics cards there are new generation 10 nvidia graphics cards coming out and some amd cards\n"