Computer Science Teacher
Computer Science Teacher - Thoughts and Information from Alfred Thompson

March, 2007

  • Computer Science Teacher - Thoughts and Information from Alfred Thompson

    Programming Proverbs 13: Do not recompute constants within a loop


    This was big in it's time because compilers were pretty dumb back then. If you added one to three inside a loop that ran a thousand times then the computer would add one to three a thousand times. Hopefully it would come up with four each time. This was of course very wasteful of time and processing power.

    This was actually done deliberately at times in order to get the computer to "wait" for the user. This was not the best way to do thing of course but it was easy and my changing the counter for the loop one could shorten or lengthen the wait. Of course as soon as you upgraded your hardware the timing all changed.

    More often this sort of thing happened accidentally when the programmer did not optimize what they are doing. This is an example where very often intermediate variables can be very useful. For example look at the following formula:

    price[i] = cost[i] + overhead + markup;

    If markup and overhead have constant values or at least values that are not changed inside the loop there will be extra additions that are unnecessary. Modern optimizing computers will see this and "fix" it for the programmer. The compiler will create its own intermediate variable, do the addition and save the results for later use. This greatly improves performance.

    Years ago I worked for a company that created a very advanced optimizing FORTRAN compiler. A customer had a benchmark program that it used to test the speed of computers and asked us to run it on our latest computer. The benchmark ran in a split second which was much faster than the large fraction of an hour that they expected. The reason was that the program used a lot of recomputation of constants and other operations that were not good coding practice. For example they added one to a variable 1,000,000 times inside a loop. The compiler looked at that and replaced all of the loops and calculations with simple assignments that saved the results and displayed  them. The compiler "solved the problem" and skipped the running the program part.

    Today compilers are even better at optimizing code so one might be tempted to suggest that recomputing constants inside a loop is not something one has to worry about any more. To some degree that is true. Still I think that it is better not to depend on the compiler to fix everything for you. It is better in my opinion to present the compiler with the best possible code.


    This is the thirteenth in a series of posts based on the book Programming Proverbs by Henry Ledgard. The index for the series is an earlier post and discussion of the list as a whole is taking place in the comments there. Comments on this "proverb" are of course very welcome here.

  • Computer Science Teacher - Thoughts and Information from Alfred Thompson

    Learning things the hard way


    One of the first things I did when I took over as Technology Coordinator for my last school was to start putting together a backup plan. We eventually had nightly incremental backups and weekly full backups. We had several weekly backups at any given time. We backed up to disk, tape and CD. Some backups were stored offsite. Others in a large fire safe.

    And the other thing we did was to regularly test the backup media so that we knew they could be read. We were able to recover data for students, faculty and administrators from our backups on a number of occasions. Surprisingly we had the most calls for data recovery from administrators but let's not get into that right now.

     Apparently they didn't do such a great job of backups in Alaska. Someone formatted the wrong two disks (main and backup) and only then did they find out that the backup tapes were unreadable. A lot of time and money went into recovering the data from paper. It is a good thing they hadn't thrown away the paper.

    I've almost always bordered on paranoid about my data. Even when I am working on systems that do automatic system backups I tend to keep a private backup of my own data. This has served me well on several occasions. It always amazes me when people have head crashes or other system problems and lose large amounts of data. Don't they backup? Apparently not. A number of times I have brought hardware in for repair and the technicians ask me about the data. When I tell them not to worry and that I have it all backed up they are almost always surprised. I guess most people don't react that way.

    I'm not sure where in the curriculum backing up computer data should be taught but I am pretty convinced that it must be taught somewhere. You'd like to think that the need for backups would be obvious but it doesn't appear to be. Or people think they will get to it later.  But later often comes too late. I'd rather see students learn this from the mistakes of others and not from their own mistakes. How about you?


    Technorati tags: , ,

  • Computer Science Teacher - Thoughts and Information from Alfred Thompson

    AP CS Conversation Follow Up


    My post on the AP CS exam gathered a bunch of comments. Some of them on the post itself and some of them on other blogs. Below I link to the other blog posts that I know about so far. Join the conversation here or there or elsewhere. 

    Brian Scarbeau - Brian is pretty frustrated with the AP CS exam. between changes for new language features and colleges not giving credit for it and 11 years of struggle under his belt he's about ready to let someone else take over.

    Kathleen Weaver - Kathleen agrees that there is too much material in the AP CS courses. She likes the case study though and doesn't want to see the language change again. She likes Java because of all the free materials that are available. She would like to see universities provide more help for HS CS teachers though. I agree with her on that one.

    In fact I think that more college professors should step forward and help grade the AP CS exam. Perhaps if more of them did that they'd have a better appreciation for what goes into that course and more kids would get credit.

    Chris Higgins - Chris has seen the exam from both sides. He took it as a student and is now teaching it. As a new teacher he is working hard to do right by his students. He's not that impressed with the need for the case study but he did find that he was way ahead (after taking AP CS as a student) when he got to college.

     Dave Jacobus - Dave doesn't mind Java but does miss being able to use Visual Studio in his AP CS course. The new features in Java that the test requires have not been implemented in J# (Java language for .NET) and that means he has to use other tools that he doesn't find as helpful.


    Technorati tags: , ,

Page 2 of 10 (28 items) 12345»