Optimisation

CPU

We could use perf and flame graph which visualise the CPU time usage by the program in its lifetime to investigate whether there are opportunities in improving CPU time.

Be aware that when measuring the program execution time or CPU cycle, it will inevitably introduce biases into the measurement since the program or code that used for the measurement will need the time for themselves too.

Memory

For JavaScript and its variants, we could use the inspect option provided by node to visualise the memory usage by the program in its lifetime to investigate whether there are opportunities in improving memory usage.

Sometime, improving memory usage could drastically improve the performance of one program.

Layout and Multithreading

The layout of a program could affect its own performance regardless of its own code. This means that the link order (changes of function address) and environment variable size (moves of program stack) could be arbitrary for one architecture which resulted in more or less cache conflict. Therefore, we need to eliminate the layout differences by using a stabiliser, which randomises the memory layout of the program, analyse it using 202205072243# and use causal profiler such as coz especially for programs that utilise parallelism#.

While using causal profiler, doing a virtual speed-up for the program (slow down every component except for the component that is aimed to speed-up). Measures which speed-up of a component is significant.

To-do: Learn more about casual profiler and stabiliser. Read the paper Layout Biases Measurement.

Note on -O3

The option -O3 from most C/C++ compilers does not actually optimise the program that much. Its effect on the program’s performance is not significant.

References

Links to this page
  • register

    register is a C keyword to hint on the compiler in order to store the variable in processor register instead of in memory. However, this can only happen in the function scope, that is within the function itself. It is advised to use it on frequently used variable in order to optimise# the performance.

  • The Pragmatic Programmer

    Refactoring is rewriting, reworking and re-architecting. Do it when there is a duplication (violate the DRY principle), non-orthogonal design, outdated code (change of requirements, better understanding) or 202203011139 opportunities (improve performance).

    Code profilers# can be useful to count the number of times the different steps in the algorithm get executed. Some might even provide a graphical plot with the information against the input size.

  • Refactoring: Improving The Design of Existing Code

    Although refactoring come at a cost of performance penalty due to indirection, it opens opportunities for fine-tuning since the code is more readable than ever. With refactoring, components tend to be small and easier to test. This means that it is easier to find out which part of code needs to be optimised which is measured by profiler since they are rather decoupled.

    It is similar to Optimisation, however with a different purpose. Optimisation is to speed up the program but might end up with less comprehensibility of the codes whereas Refactoring wants to make the software easier to understand and extendible.

  • Refactoring

    Refactoring is a process of rewriting, reworking and re-architecting of the program’s codebase without changing its observable behaviour. It is often used to prevent or correct code duplication#, preserve the orthogonality# of the software, update the code base on the changes of requirements or better understanding on the underlying technology, or optimise. The ultimate goal of Refactoring is to make the software easy to read, have all logic specified in one and only one place (no duplications#), doesn’t allow changes to alter existing behaviour, and allow only simple conditional logic.

    Refactoring speeds up the software development by making the process of understanding a codebase easier. The code should be readable so the next guy who picks up the codebase will not waste much time on comprehending the code before becoming productive. This is with extra benefit where we can spot bugs more easily as we tend to spend more time in debugging#. Make the code speaks its purpose with more clarity. But it might drag the performance due to indirection.

  • Inline Function

    A function with the identifier inline in C is called an Inline Function. Inline Function makes no function call, instead they are like macros where the place that the function called will be replaced by the function definition or the code that is inside the function. Since inline function doesn’t introduce #overhead, it is great to inline short functions especially 202202081524#. Always prefer Inline Function over function-like macro.

  • C++ Container

    The default 202205161742# for containers are new and delete. You could change it in order to optimise the memory usage.

  • C Hygiene
    Avoid taking local variables addresses using & operator as this will inhibit 202205171500# which is great in 202203011139#. See 202205171504.
  • Aliasing

    Aliasing is when different symbolic names can access to the same object that is stored in the memory. This could be a hassle for 202203011139#.

#memory #multithreading