    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Increase your Program's Execution Speed?</title>
        <link>http://accu.org/index.php/journals/717</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">CVu Journal Vol 8, #1 - Feb 1996 + Programming Topics</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="http://accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="http://accu.org/index.php/journals/c76/">Journals</a>

                     &gt;                         <a href="http://accu.org/index.php/journals/c77/">CVu</a>

                     &gt;                         <a href="http://accu.org/index.php/journals/c137/">081</a>
                    (7)
<br />

                                            <a href="http://accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="http://accu.org/index.php/journals/c13/">Topics</a>

                     &gt;                         <a href="http://accu.org/index.php/journals/c65/">Programming</a>
                    (488)
<br />

                                            <a href="http://accu.org/index.php/journals/c137-65/">Any of these categories</a>

                    -                        <a href="http://accu.org/index.php/journals/c137+65/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Increase your Program's Execution Speed?</h1>
<p><strong>Author:</strong>&nbsp;Administrator</p>
<p>
<strong>Date:</strong> 03 February 1996 13:15:26 +00:00 or Sat, 03 February 1996 13:15:26 +00:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e23" id="d0e23"></a></h2>
</div>
<p>I asked The Harpist to add a commentary because the subject is
one where experience and awareness of changing conditions is
important, <span class="emphasis"><em>Francis</em></span></p>
<div class="sidebar">
<p class="title c2">Introduction from The Harpist</p>
<p>When Francis asked me to look at this article and annotate it I
was reminded of two things. The first is a book by David Spuler
(C++ and C Efficiency, if memory serves me correctly) which tackles
many optimisation issues. The problem with it is that David Spuler
is an academic whose basically excellent writing is sometimes
marred by a lack of currency perhaps as a result of his view from
the ivory towers of academia. It is easy to propose apparently
sensible ideas to help optimise some aspect of your program, only
to make matters worse because you get int the way of modern
optimisation technology.</p>
<p>The second thing I was reminded of is Mike Banahan's excellent
presentation on the subject where he relates his attempts to
improve the performance of a utility that he had written
(originally in AWK, I think). Every idea for optimisation that he
proposes seems entirely sensible and likely to speed up the program
significantly. And every one causes the program to run slower.</p>
<p>The moral is to avoid hand optimisation unless the program needs
a better performance. When it does, start by checking your choice
of algorithms and follow that with a careful search for
bottlenecks.</p>
<p>Enough of this. Let us proceed with the original submission. If
I appear to be critical, excuse me. I think that material such as
the following is important because it brings problems to the
surface. My prognostications are in this type-face the other is
material from Silas.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e40" id=
"d0e40"></a>Introduction</h2>
</div>
<p>Note that these tips are in no particular order. If anybody has
anything that they could add to this, why not write in? Also,
please note that these tips are for increasing program execution
speed, which may be at the expense of program maintainability,
reusability, portability, and possibly (almost certainly) size as
well.</p>
<p><span class="bold"><b>1.</b></span> Think about the types which
you are using for variables. Characters are sometimes twice as fast
as integers, and if integers stay in their range, they could be
used instead. If you're concerned about your program looking nice
with descriptive types, you could use #define or typedef, eg.
#define shortNumber unsigned char.</p>
<div class="sidebar">
<p>Indeed, think about the types. Understand the original design
criteria for the built-in types of C. It will be rare that the use
of <tt class="literal">char</tt> for small integers will improve
speed. <tt class="literal">int</tt> is intended to be the natural
integer for the underlying hardware and will usually be the fastest
performer. Use <tt class="literal">short</tt>, or possibly
<tt class="literal">char</tt> where minimal storage is important,
use <tt class="literal">long</tt> where a large range of values is
required. Note that arithmetic is often provided for <tt class=
"literal">chars</tt> by promotion to <tt class="literal">int</tt>.
NEVER use a plain <tt class="literal">char</tt> for a minimal
integer as its behaviour is system dependant (can behave as either
<tt class="literal">signed</tt> or <tt class=
"literal">unsigned</tt>).</p>
<p>If you must use some form of <tt class="literal">char</tt> to
store integer values, do please use a <tt class=
"literal">typedef</tt> or a <tt class="literal">#define</tt> as
that will allow you to quickly change the true type. Note that
having made such a choice you better not try to use either
<tt class="literal">scanf()</tt> or <tt class=
"literal">printf()</tt> functions because you will have a type
incompatibility between the format string and the data.</p>
</div>
<p>Also, avoid floating point numbers like the plague - they can be
eighty times slower than characters, unless you have a floating
point co-processor. You could multiply the value by a large number
and process it in terms of that, and the extra code needed to do so
shouldn't take nearly so much time as the floating point
arithmetic. For example, instead of multiplying something by 0.8,
multiply it by 4 and divide it by 5, or, instead of multiplying
something by x where x needs accuracy of 0.01, multiply by 100x and
divide by 100. Think carefully about whether you would store the
<tt class="literal">floats</tt> in <tt class="literal">ints</tt>,
<tt class="literal">chars</tt>, or <tt class=
"literal">longs</tt>.</p>
<div class="sidebar">
<p>True, without a FPU, floating point can be a bottleneck but so
is integer division. Very few systems can get within a factor of
ten between multiplication and division. For this reason programs
that find speed a vital issue usually resort to using fixed point
binary. A good example of this can be found in FRACTINT. You almost
certainly will need to consider writing assembler level
routines.</p>
<p>Using hand-coded fixed point arithmetic is tedious and usually
your time would be better spent on something else.</p>
<p>Actually, one of the greatest time consumers is using a denary
system that simply does not translate well onto the binary
mechanisms of the hardware.</p>
<p>In the first example above, what type is being multiplied by
0.8? If it is a float then you will only lose by exchanging one
operation for two. If it is an integer, then why are you
multiplying it by a float? Consider:</p>
<pre class="programlisting">
int x;
int temp;
/* code initialising x */
temp = (x&gt;&gt;1 + x) &gt;&gt; 1;
// temp is now 3/4 of x
temp= temp + temp &gt;&gt; 4 + temp &gt;&gt;8;
// temp is now  0.7998046875 of x
</pre>
<p>Which is probably close enough for most cases but you can always
add in more terms if you need to.</p>
</div>
<p>On the subject of types, type conversions often take rather a
lot of time as well, so try to avoid them when they are not
necessary. For example, I have often seen code which has a function
which takes, say, ints as parameters, and most of what it does to
them is pass them to another function which takes chars as
parameters. This means that, if the program passes chars to the
function, it has to convert them to ints and back to chars and all
for apparently no reason (there could be exceptions, of
course).</p>
<div class="sidebar">
<p>Oh! Dear! How do you think a <tt class="literal">char</tt>
argument is passed in C? As an integer (ISO C with prototypes may
provide some exceptions to this) so passing <tt class=
"literal">char</tt> arguments to <tt class="literal">char</tt>
parameters usually results in two type conversions [one at the call
site (<tt class="literal">char</tt> to <tt class=
"literal">int</tt>) and one at the execution site (<tt class=
"literal">int</tt> to <tt class="literal">char</tt>)]. In general,
if speed matters use <tt class="literal">ints</tt> consistently
instead of <tt class="literal">chars</tt>.</p>
</div>
<p><span class="bold"><b>2.</b></span> Try doing various things
with the compiler. Some compilers have an &quot;optimise for speed&quot;
option, and turning this on sometimes helps. Be careful about
compiler optimisations, though - they may introduce non-standard
exceptions in areas such as type conversion. I have seen a &quot;fast
floating point&quot; option, which, while it does speed up the floating
point, inhibits all conversions, explicit or implicit, between
floating point and non-floating point numbers.</p>
<div class="sidebar">
<p>Yes, there are quite a few old compilers that have silly
optimisations, but if you must do mixed mode arithmetic the burden
of care lies partly with you. If the compiler really ignores
explicit (cast) conversions return it as it is dangerously
broken.</p>
</div>
<p>Also, look for options like &quot;Automatic register variables&quot; to
automatically store variables in CPU registers when they can, or,
better, turn these options off, and use the register keyword to
pick the best variables which could be register variables - the
ones most often used.</p>
<div class="sidebar">
<p>It is a very rare programmer who can better a modern compiler in
its choice of when to use a register. To make the optimal choice
you would need to know how many registers are currently available
for use so that you did not specify more than available with the
possible consequence that the one that gets put somewhere else is
the one that really needed to be in a register. As regards register
qualified parameters, it will almost certainly ignore you (which it
is entitled to do).</p>
<p>By the way the ISO C standard has very little to say about the
meaning of <tt class="literal">register</tt>. And C++ even removes
the prohibition against taking the address of a <tt class=
"literal">register</tt> variable.</p>
<p>You should also note that many C++ compilers make extensive use
of registers to support reference variables.</p>
<p>In general these days leave registers to the compiler.</p>
</div>
<p>You could try running your program through an executable file
compressor such as PkLite or LzExe - this may improve the loading
time, and some compressors also have the option to optimise the
header. Make sure that you turn the debugging information off when
you do this.</p>
<div class="sidebar">
<p>This is very hardware dependant and is only really worth
considering if you are going to execute programs as part of another
program.</p>
</div>
<p>It is a good idea to turn the debugging information off anyway
unless you really need it. Most debugging jobs I have come across
could be done with a print statement in the right place, and
anyway, I don't know about anybody else but my debugger tends to be
rather unreliable. On the subject of debugging, beware testing
routines which take time themselves - I have in the past spent
weeks trying to optimise a really slow piece of code, and it turned
out that what was really slowing it down was the clumsy algorithm I
put in to time its execution speed!</p>
<div class="sidebar">
<p>That I think says more about the kind of code you write than
anything else. Debuggers provide three valuable services:</p>
<div class="variablelist">
<dl>
<dt><span class="term">a)</span></dt>
<dd>
<p><span class="emphasis"><em>help you locate bugs in your
code</em></span></p>
</dd>
<dt><span class="term">b)</span></dt>
<dd>
<p><span class="emphasis"><em>help locate errors in the
compiler</em></span></p>
</dd>
<dt><span class="term">c)</span></dt>
<dd>
<p><span class="emphasis"><em>allows you to force execution of all
your code</em></span></p>
</dd>
</dl>
</div>
<p>The last of these is very important. An error handling routine
that has never been checked is next to useless. Debuggers allow you
to change the value of critical variables and thereby force
execution down little used paths.</p>
<p>By the way, you should check out profilers if you need to
identify bottlenecks in your code.</p>
</div>
<p><span class="bold"><b>3.</b></span> Think about the structure of
your program. Don't forget that the program that you design to make
readable need not be the fastest implementation. Try to avoid using
virtual functions in classes, since these can slow down execution
as the program has to decide which function to call. Avoid
unnecessary recalculations by storing the values in variables, for
example, <tt class="literal">for (int i=0; i&lt;=10; i++)
printf(&quot;%d\n&quot;, myfunc(2*b));</tt> could be made quicker by
<tt class="literal">for (int i=0, int j=myfunc(2*b); i&lt;=10; i++)
printf(&quot;%d\n&quot;, j);</tt> Be careful, though, whThe last of these is
very important. An error handling routine that has never been
checked is next to useless. Debuggers allow you to change the value
of critical variables and thereby force execution down little used
paths.en doing this - some calculations are quicker left as they
are, and sometimes the value may be different each time, in which
case, leave it!</p>
<div class="sidebar">
<p>Did you run your code through a compiler? I thought not because
even C++ only allows you one declaration in the init clause of a
for statement. A good optimising compiler will pull out common
code, so generally leave code where it makes sense. However you
should be aware of things that a correctly functioning compiler
cannot do for you. Consider:</p>
<pre class="programlisting">
void fn (const int * cpi, int * pi){
  *pi+= *cpi;  // line A
  printf(&quot;%d&quot;, *cpi);  // line B
}
</pre>
<p>Despite the fact that cpi points to a const int the compiler
cannot use the value it has loaded into a register at line A when
it comes to execute line B! Why? Well <tt class="literal">pi</tt>
and <tt class="literal">cpi</tt> may both point to the same
address. This is called the aliasing problem.</p>
<p>The aliasing problem is even worse in C++ because reference
parameters may be aliasing parts of other parameters that have a
user defined type. (Strictly speaking this can also be a problem in
C where parameters are of type pointer to <tt class=
"literal">struct</tt>). Note that the aliasing problem is a general
one and not dependant on a parameter being <tt class=
"literal">const</tt> qualified. The point is that there is no way
of providing optimisation for much of the use of parameters.</p>
<p>In the above code, exchanging line A and line B will allow an
optimisation - the value of <tt class="literal">*cpi</tt> only has
to be fetched once.</p>
</div>
<p>In some cases when a function performs a calculation based on
its parameter and nothing else, and the parameter can be of a
limited number of values, it may be better to store the values
given by the function at the start of the program for any of the
parameters used. For example, if you needed to calculate the sines
of integer angles between 0 and 360 degrees use a statement like
&quot;<tt class="literal">#pragma startup</tt>&quot; (Borland C++ 3.0) or
something similar to execute a function when the program starts
which initializes an array with the values necessary, obtained by
calling the functions if you like - or you could even embed the
code into the executable, and then just use the array. An example
follows, which is also an example of avoiding the use of floating
point:</p>
<pre class="programlisting">
const float deg2rad=180/M_PI;
long sine[360], cosine[360];
void initTrig() {
  for (float lp=0; lp&lt;360; lp++) {
  // lp is float because of trig functions
    sine[lp]=sin(lp/deg2rad)*65536L;
    cosine[lp]=cos(lp/deg2rad)*65536L;
  }
}

#pragma startup initTrig
#define angleRange(angle) \
  while ((angle)&lt;0) (angle)=(angle)+360;\
  (angle)=(angle) % 360

// rotation about the origin-not a float in sight
void rotate(long &amp;x, long &amp;y, int angle) {
  angleRange(angle);
  long x2= y*-sine[angle]/65536L+x*cosine[angle]/65536L;
  y=x*sine[angle]/65536L+y*cosine[angle]/65536L;
  x=x2;
}
</pre>
<div class="sidebar">
<p>Sorry, but the above code is an excellent example of how
<span class="bold"><b>NOT</b></span> to optimise.</p>
<p>Lookup tables are a superb tool for optimisation. Francis'
implementation of FORTH contains a staggeringly fast trig
vocabulary (sort of like a library) that uses fixed point
arithmetic coupled with a single table of 256 4-byte values and
linear interpolation to provide limiting accuracy for sines and
cosines (as well as their inverses).</p>
<p>The problem is with the rest of your code. Let me start with
your lookup tables. Why two? Why 0 to 360 degrees? You only need
one cycle (actually symmetry allows half a cycle, but let me keep
it simple) of either sine or cosine (as a phase shift of 90 degrees
will handle the other). To minimise calculations I am going to use
a table for sines from 0 to 270 degrees. The sines of 0 to 180 can
be extracted directly and the cosine of x will be the sine of x+90.
You can provide a macro or inline function to manage the
relationship between sine and cosine if you wish.</p>
<p>Now look at your <tt class="literal">angleRange()</tt> function.
In C++ I'd make it inline but if you want to use C then by all
means us a pre-processor macro (I do wish you would make up your
mind as to which language you intend to use), but spend the time to
get it optimised.</p>
<pre class="programlisting">
inline void angleRange(int &amp; angle){
  angle %= 180;
  if (angle&lt;0) angle += 180;
}
</pre>
<p>Check the integer division rules for your compiler because those
that round towards minus infinity can use:</p>
<pre class="programlisting">
inline void angleRange(int &amp; angle){
  angle %= 180;
}
</pre>
<p>for which you could consider:</p>
<pre class="programlisting">
#define angleRange(x)  x%180
</pre>
<p>if you are using a macro.</p>
<p>Finally look at your rotate function and compare it with the
code below.</p>
<pre class="programlisting">
void rotate(long &amp; x, long &amp; y, int angle) {
  angleRange(angle);
  long x2= (x*sine[angle + 90] -y*(sine[angle]) &gt;&gt; 16;
  y=(x*sine[angle]+y*sine[angle+90]) &gt;&gt; 16;
  x=x2;
}
</pre>
<p>Which eliminates the divisions and reorganises the order of
evaluation to ensure a minimum number of arithmetic operations. It
also removes your syntax error.</p>
</div>
<p>It is sometimes worth using macros instead of small functions,
as there is an overhead in calling a function. A function which
added its two arguments, for example, would be better as a macro.
How much execution time you could save here is dependent on how big
you don't mind your program getting - if you implement larger
functions as macros, you may save more time, especially functions
which are called often. Or, you could include the source in your
main module - optimising at each call. There are disadvantages in
this approach: if you were to change the function, you may run into
problems, and there are also problems with static variables. If you
run the program through an executable file compressor, there
shouldn't be too many problems with the size, unless you run out of
memory - executable file compressors rarely cope with overlays.</p>
<div class="sidebar">
<p>Look your code uses reference variables and so is C++, so what
is this macro business?</p>
<p>If a function is large enough to cause code bloat when it is
inline (or a C macro) then the gain you get from avoiding a
function call will generally be too little to make even a single
percentage point's difference to the overall application.</p>
</div>
<p>What I've just been saying is perhaps going a bit too far,
though it may be used in a part of the program where speed is
really essential, at the sacrifice of maintainability. However, if
possible, try to avoid functions like the following, which may be
better as several different functions, depending on how it is
called:</p>
<pre class="programlisting">
void myfunc(int whatToDo) {
  switch (whatToDo) {
    case 1: /* this */ break;
    case 2: /* that */ break;
    case 3: /* the other */ break;
    default: printf(&quot;Bug!\n&quot;);
  }
}
</pre>
<div class="sidebar">
<p>If a decision has to be made, the use of a switch statement will
become optimum when the number of options reaches about five. That
leaves the overhead for the function call. I think this case is too
artificial to think about.</p>
</div>
<p>Conditionals also take time - in some small loops, if the value
is likely to be the same, it is worth writing out the loop twice,
and putting an &quot;if else&quot; statement around the whole lot, rather
than writing the loop which consists entirely of an &quot;if else&quot;
statement. For example, the following:</p>
<pre class="programlisting">
for (int i=0; i&lt;=10; i++)
  if (j==2) {
    /* this */
  } else {
    /* that */
  }
</pre>
<p>may be quicker when re-arranged as follows, depending on how big
you don't mind your program getting (which is why I said SMALL
loops):</p>
<pre class="programlisting">
if (j==2)
  for (int i=0; i&lt;=10; i++) {
    /* this */
  }
else
  for (int i=0; i&lt;=10; i++) {
    /* that */
  }
</pre>
<div class="sidebar">
<p>Again, if code bloat begins to be significant the gain from the
technique is probably too small to worry about.</p>
</div>
<p>Don't forget that there can be problems with maintenance, and
also to leave it alone if the conditional is likely to change
within the loop. Remember, the purpose of optimisation is not to
change the function of your program: if in doubt, leave it alone,
or at least keep a backup of your original code. In case something
does go wrong, it may be worth trying one change at a time only, so
you may be able to find and solve the problem quicker.</p>
<p><span class="bold"><b>4.</b></span> Choose carefully which
standard library functions you are going to use. Standard library
functions are sometimes designed to work in a variety of ways,
sometimes checking for things which your program doesn't really
need. For example, if you are writing a game which involves
printing to the screen, do you really need to use a function which
checks to see if output is being redirected to a file? It seems to
me that, in this situation, the likelihood of the user wanting to
redirect a player walking across the screen is not really worth the
extra time the check takes - why not use a different function
instead? If you are quite concerned, you could always add a command
- line option to your program to use the old calls, but try to
avoid too many conditionals. And why use a function with
capabilities of formatting the text when you don't need it to be
formatted, or writing in different fonts, etc. when it is not
necessary?</p>
<p>Some standard library functions on some compilers rely on
external variables - pay attention to these. For example, there may
be a variable specifying whether to use BIOS calls or direct screen
writes. In some cases, it may be better to write your own
functions, at least for parts which need to be quick. If you
program in Assembler then that's all the better.</p>
<div class="sidebar">
<p>Are you talking about ISO (aka ANSI) C Standard Library
functions? If so, it goes without saying that you should understand
enough to recognise which are inefficient (largely high level i/o
functions). If you are going to provide your own specialist
functions, keep them in a file of their own so that they can be
replaced easily.</p>
<p>I think you exaggerate the cost of redirection. This is usually
very cheap.</p>
</div>
<p><span class="bold"><b>5.</b></span> Think about the data
structures of the program. Try to avoid using the heap whenever it
can be avoided. Use of new and delete takes time. Also, it may be
worth considering declaring some data static in functions where it
is often used, if speed is really necessary. Try to create as many
of the program's objects as possible at the start of the program,
and destroy them at the end - but don't forget the impli-cations
this may have for memory requirements. When deciding a data
structure, there are several things to consider, including the
speed of accessing the data within the structure (a linked list may
take a while), and the speed of altering the structure if necessary
(it may take some time to insert data into an array). How the
structure is to be used is quite important here.</p>
<div class="sidebar">
<p>You can also consider providing your own versions of new and
delete which use a cache of memory that your program has grabbed at
startup.</p>
</div>
<p><span class="bold"><b>6.</b></span> Concentrate on the
bottlenecks. Try not to waste time optimising code which decodes
the command-line arguments, for example, if it's only called once.
(If this example is not only called once, why not?) The code which
needs to be optimised is usually the code that would make the most
difference if it were optimised. It may be worth concentrating on
the parts of the program where speed is necessary to the greatest
degree. For example, if programming a game, speed is ideal in
routines to play the game, but not so necessary in the editor.
There are probably better things to concentrate on when writing a
music transcription program than making sure that files are saved
to disk in as short a time as possible. The time it takes a
database to load doesn't usually matter as much as the time it
takes to change records.</p>
<div class="sidebar">
<p>Exactly right - focus on the most sensitive areas of your code.
Develop techniques and acquire tools that will allow you to
identify these areas.</p>
</div>
<p><span class="bold"><b>7.</b></span> It may be worth trying to
put speed in its proper place. There are other things to consider
when writing a program. As an example of this, I have heard of a
music program which can draw a score in 0.1 seconds, which may be
ideal for some applications, but the way in which the program is
written may pose problems if the authors wanted to port it to
another platform, or add a major feature to the drawing algorithm
having neglected it for long enough to be not sure about how it
works. Also, what if the authors were to suddenly die, and nobody
else could work out what the program was about? Size is another
thing, though not in this example, and also, again not necessarily
in this example, the time it takes one to write a program as well,
or possibly the time it would take to use some of the routines in
other programs. More specialised routines can hinder reuse, but can
also increase execution speed.</p>
<div class="sidebar">
<p>Or more simply, don't waste time optimising code that is already
fast enough. The most expensive time in many programming
environments is that of the programmer - something that many
companies are now finding when faced with virtually unmaintainable
code.</p>
</div>
<p>Well, that's it. If anybody has any more suggestions, it may be
worth writing in - there is no need to assume that everybody in
ACCU knows everything you do and more. If you think that there's
something wrong, please write in as well, for the benefit of
readers. And please don't forget the scope of the article, as I
keep saying - it is to do with increasing the execution speed of
programs, and there may be problems involved in some of the
methods. If this isn't clear, people may start writing programs
which are quite fast, and then starting again whenever there's a
bug, and other people may take offence at seeing some of these
methods in print. By the way, for those who would, this isn't the
worst that you can get - to end this article, here is a snippet of
BBC BASIC code I once saw in a magazine (and I understand that the
company which used to publish the magazine is a member of ACCU, but
please don't take offence, those were the old days):</p>
<pre class="programlisting">
1040FORI%=A%TOA%+287STEP21:$I%=STRING$(20,CHR$7)+CHR$0:NEXT
1050A%?166=0:COLOUR1
1330FORJ%=1TOI%-1
1340IFX%(I%)&lt;&gt;X%(J%)GOTO1360
1350IFK%=0K%=J%ELSEK%=-1
1360NEXT
1370IFK%=0PROCNL:GOTO1420
1380IFK%=-1GOTO1320
1390U%(I%)=U%(K%)-3:L%(I%)=31-L%(K%)
1400IFU%(I%)&lt;12ANDL%(I%)&lt;12GOTO1320
</pre>
<p>Sorry I can't put the complete listing in, but it's full of code
like that! The programmer must have had considerable skill
debugging it. Oh, and I've just got to include this one:</p>
<pre class="programlisting">
2200 IF?P%=9 AND HT%(Q%)=0 THEN HT%(Q%)=1:VDU23,246+Q%,0,0,060,60,60,60,255,255:?P%=7:A%?(1+17*Q%)=10+Q%:COLOUR3:PRINTTAB(MX%(Q%),MY%(Q%)&#8209;1)CHR$(246+Q%):SOUND1,1,200,20:ENDPROC
</pre>
<p>Now we can agree. I always hated the way so many people wanted
to hype up BBC BASIC when there were already much better compiled
BASICs the other side of the Atlantic.</p>
<div class="sidebar">
<p class="title c2">Conclusion from The Harpist</p>
<p>If you want to learn to write code that flies, you must study
data-structures and algorithms. It is nearly always a matter of
choosing a better combination rather than tweaking code.</p>
<p>If speed really matters think carefully about your choice of
programming language. C++ can produce very fast code when used by
an expert, it can also produce surprisingly slow code when used
without deep understanding.</p>
<p>C can produce close to assembler level speed and size and your
benefit should be in maintenance and portability.</p>
<p>Don't optimise what does not need it, your time is too
valuable.</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
