The area of code optimisation has long interested me.
I started off my programming career in assembly language on the venerable BBC Microcomputer, one of my first exercises was to convert an efficient implementation of Conways game of Life, the cellular automation game that had been written in BASIC to 6502 assembly language.
My initial implementaion had a bug! a test that whether a cell was worth calculating caused the machine code to always calculate every cell. The net result was a program that worked, a program that was inherently perhaps a 100 times faster than the BASIC version did in fact operate marginally slower.
This bought home to me the importance of using the correct algorithms and efficient programming.
Later in my career I worked using Alex an interpreted language, but one that does have a callable API allowing you to avoid the slowness of any interpreter. In this particular project, the language. Alex used almost all the right algorithms and extensive profiling revealed very few black spots. Despite this it did run at too slow a speed for the project in question. extensive work on just about all section of the code did bring about a near doubling of the speed.
This made me question the old adage of optimisation, which is essentially don't waste time on it, but at the end of a project profile and deal with the problem areas. I think that with big projects you should always be keeping at least a weather eye on optimistion, as sometimes the small issues mount up, and to have the intelligence to see the obviously key areas of a project in advance and not to be left trying to "hack" in optimsations at the last.
Using C++ in particular raises a number of interesting aspects to opimisation. In C++ you tend to operate at a much higher level than in C. When you use a null terminated string in C there is no question in your mind as to what you are getting for your money. You know for example that if you write:
for (i = 0; i < strlen(strText);i++){
}
then it is going to be very inefficient. But when you are in C++, just how bad is:
for (i = 0; i < strText.length();i++){
}
It is these questions and others that I intend to try and address here.
|