Here, some general tips for getting better code are presented. They mainly concern coding
- Find a better algorithm. No matter how much you and the compiler tweak the code, a
quicksort will (almost) always outperform a bubble sort, for example.
- Use variables of the native size of the processor you’re writing for. This is currently
32-bit or 64-bit for Free Pascal, so you are best to use longword and longint variables.
- Turn on the optimizer.
- Write your if/then/else statements so that the code in the ”then”-part gets executed
most of the time (improves the rate of successful jump prediction).
- Do not use ansistrings, widestrings and exception support, as these require a lot of
- Profile your code (see the -pg switch) to find out where the bottlenecks are. If you
want, you can rewrite those parts in assembler. You can take the code generated by
the compiler as a starting point. When given the -a command line switch, the compiler
will not erase the assembler file at the end of the assembly process, so you can study
the assembler file.