Most programmers write code very inefficiently. In fact as programming languages become more high level programmers become more lazy. Most programmers do not even know, or care, that their code is inefficient. Thankfully the complier does care and will perform a number of optimisations for the user. It is outside the scope of the course to examine exactly how these optimisations work. Simple examples will be to reduce the number of jumps the code has to do. Also reduce the number of procedure calls. A linear program with no jumps will always run faster than a heavily procedural program. Obviously programming in this way is not feasible and will introduce a greater chance for bugs, much increased source code and also much higher chance that bugs will not be found. As we can rely on the optimiser to do this work for us we do not need to compromise our coding style for the sake of speed. However it is always worth remembering that IF statements, loops and procedure calls are inherently slow so should be used with some degree of caution.

  • Optimizing will reduce the size of the generated code
  • Optimizing will speed up the generated code (not the compiling process!)
  • Will get rid of un-nessesary lines of code
  • Will recognise certian code patterns and replace them with more efficent ones.

Note- Some short procedures are turned into inline procedures. What this will do is basically copy and past the contents of the inline procedure to every call of it.

 

For example

  1. X(int a)
  2. A = a+ 1
  3. Return x
  4. End
  5. Z()
  6. B = 1
  7. B = X(B)
  8. B = B * 2
  9. B = X(B)
  10. Return B
  11. End

 

If we make X inline, the code becomes -

 

  1. Z()
  2. B = 1
  3. B = B + 1
  4. B = B * 2
  5. B = B + 1
  6. Return B
  7. End

 

In truth the code will not look exactly like this due to the way the compiler does this. However the above example gives a good enough idea of what happens.