Help save the Earth and our lives - write faster code!


C# is a very fast language. Unfortunately it is very easy to write a slow code in C# and many people really take the opportunity to do it. Running faster code, the computers will use less energy, so less C02 and toxic gases will be released to atmosphere by power stations, fewer servers have to be produced and installed in server farms, which will also reduce the impact on environment and consequently we will live healthier and longer. Help save the Earth and our lives - write faster code!


  1. SIMD instructions (vector) and multitasking/multithreading are a must and are the things you should start writing you program from in order not to forget about them later.

  2. The simplest thing you can do to make your code faster is set garbage collector in server mode. Here is how to do it: https://docs.microsoft.com/en-us/dotnet/core/runtime-config/garbage-collector. This alone can speed up your application by about 10-20%. However, at the same time the application uses more memory, but less free memory is usually is no problem.

    GC.png

  3. There are several other issues to consider to optimize garbage collector - but that is a topic for a whole book.

  4. In performance non-critical sections everything can be used - no problem. But in critical sections (usually executed many times in plots and/or accessed by many processes) several things should be used as little as possible: casting, LINQ, collections, comparisons. Jagged arrays are faster than regular arrays, object fields are faster than properties, etc.

  5. If you do need a dictionary-like collection - consider the time of item insertion and retrieval of various collections and how it matches the frequency of these operations in your code.

  6. StringBuilder and multi-threading are good and should always be used with one exception: where the cost of creating them is comparable or even higher than the cost of the operations they perform. For small amount of data they do not make sense.

  7. Usually the modern and cool constructs are also slower. It is OK to use them, outside of the critical sections. As a simple example, just try to compare the performance of string concatenation by $"a={a}" and by "a="+a.

  8. The fastest access to arrays is vector (SIMD) or range operator X[n..m]. If you can work on the original array than also Span may do the job. The fastest are arrays on stack (created with Span and StackAlloc)

  9. Sometimes (maybe even frequently) performing some additional multiplications or other operations on the data in processor cache is faster than performing fewer operations but having to transfer the data from the main memory.

  10. Accessing remote memory or remote cache in multi-cpu systems is slow. First use a single logical core per each physical core. Setting processor affinity will help in both cases.

  11. Thread or task synchronization should be performed as seldom as possible and the jobs send to them (also to actors in Akka.net) should be as big as possible, especially in distributed systems. Parallel.For should not always run on all available CPU cores; if the job is small, it will complete faster with lower MaxDegreeOfParallelism value.

  12. Dapper is a good way of accessing SQL databases. It is only a little bit slower than direct ADO.NET, but much faster than Entity Framework. An optimal structure of SQL databases with optimal indexes and well written SQL queries is another topic for a separate book.

  13. Pooling resources that can be reasonably pooled: objects, threads, connections, is a good idea, as creating them is costly.

  14. Last, but most important: design your algorithm well - so that it runs fast and does not perform unnecessary operations. A simple (maybe too simple) example below:

    for (int n=2; n<Math.Sqrt(N); n++)
    {
          x+=n*n;
    }


    Do you think Math.Sqrt(N) can change during the iterations? It not, then what is the reason to calculate it over and over again?

  15. I can also recommend Perfview - a very helpful tool in code optimization.




       







Creative Commons License. You are free to copy, share and adapt all articles and software from my web page for noncommercial purposes, provided that you attribute the work to me and place a link to my home page. What you build upon my works may be distributed only under the same or similar license and you may not distort the meaning of my original texts. Creative Commons License. You are free to copy, share and adapt all articles and software from my web page for noncommercial purposes, provided that you attribute the work to me and place a link to my home page. You are free to copy, share and adapt all articles and software from my web page for noncommercial purposes, provided that you attribute the work to me and place a link to my home page. What you build upon my works may be distributed only under the same or similar license and you may not distort the meaning of my original texts.Miroslaw Kordos