SlideShare a Scribd company logo
Achieving
Performance in
      C#
    Roman Atachiants
Contents
•   Introduction
•   Memory Management
•   Flow and Control Structures
•   Data Structures
•   Function Calls
•   Questions
Introduction
“Premature optimization is the root of all evil”
            Prof. Donald Knuth
Software
• Correct
• Maintainable
• Fast
When & Where
• One should not optimize everything prematurely
• One should keep in mind good programming practices
• One should evaluate the
The Secret
Not to do ANY work.
Memory Management
“The memory management on PowerPC can be used to frighten small
                         children”
                      Linus Torvalds
GC
• The .NET Framework uses automatic garbage collection to
  manage memory for all applications.
                                                            Memory for an
                            Object’s                            object is
                         memory is freed                    allocated from
                           (collected)                       the managed
                         some time later                    heap when you
                                                                call new




              Object dies tu to
              all its references
                                                                         Object
                 either being
                                                                      constructor is
               explicitly set to
                                                                         called
              null or going out
                    of scope



                                           Object is used
                                           for some time
Assemblies
• Prefer single large assemblies rather than multiple smaller
  assemblies
• Overhead:
   o The cost of loading metadata for smaller assemblies
   o Touching various memory pages in pre-compiled images in the CLR in order to load
     the assembly (Ngen)
   o JIT compile time
   o Security checks

• Sometimes you cannot avoid splitting assemblies; for
  example, for versioning and deployment reasons. If you
  need to ship types separately, you may need separate
  assemblies.
Memory
• Allocation:
   o Is super fast!

• Free:
   o Is super slow!

• You want to avoid freeing memory. The easiest way to do
  so is not to allocate in the first place.
Reuse Memory
                                        • Do not use a new in a loop
for (int i = 0; i < 100; ++i)             unless you really need to.
{
  var numbers = new int[10];
  // (...) Do something with numbers

    Console.WriteLine(numbers.Sum());
}



var numbers = new int[10];
for (int i = 0; i < 100; ++i)
{
  // (...) Do something with numbers

    Console.WriteLine(numbers.Sum());
}
Object Pools
public sealed class ScriptBuilder : RecyclableObject            • Object Pooling is
{
    ...                                                           something that tries to
}
                                                                  keep a pool of objects in
public sealed class ScriptBuilderPool :                           memory to be re-used
ConcurrentPool<ScriptBuilder>
{                                                                 later and hence it will
    public static readonly ScriptBuilderPool Default =
          new ScriptBuilderPool();                                reduce the load of object
    public ScriptBuilderPool() :
                                                                  creation to a great extent
          base("ScriptBuilders", _ => new ScriptBuilder()){ }
}                                                               • Object Pool is nothing but
using (var obj = ScriptBuilderPool.Default.Acquire())             a container of objects
{
    // Do stuff
                                                                  that are ready for use
}
Class Design
•   Do not make classes thread safe by default.
•   Consider using the sealed keyword.
•   Consider the tradeoffs of virtual members.
•   Consider using overloaded methods.
•   Consider overriding the Equals method for value types.
•   Know the cost of accessing a property.
•   Consider private vs. public member variables.
•   Limit the use of volatile fields.
Class vs Struct
• Class
   o A reference type
   o Lives in the heap
   o Slower

• Struct
   o A value type
   o Lives in the stack
   o Faster

• … But, never use structs that are bigger than 16 bytes. Use
  them wisely.
Threads
• Thread threads as a shared resource
    o Do not create threads on a per-request basis because this can severely impact
      scalability. Creating new threads is also a fairly expensive operation that should be
      minimized. Treat threads as a shared resource and use the optimized .NET thread
      pool.

•   Locking is slow, avoid locking large portions of code
•   Minimize thread creation
•   Use the thread pool when you need threads
•   Use a timer to schedule periodic tasks
•   Never use Thread.Abort
•   Never use Thread.Suspend or Thread.Resume
Flow & Control
 “It is practically impossible to teach good programming style to students
that have had prior exposure to BASIC. As potential programmers, they are
              mentally mutilated beyond hope of regeneration.”
                                  E. W. Dijkstra
For vs For..Each
                               • For and for .. each loops
foreach(var item in List)
                                 are different:
{                                 o For each is less performant than a
 // do something with item          for loop
}
                                  o For each creates garbage

int count = List.Length;
                               • Always prefer for loops for
for(int i=0; i < count; ++i)
{
                                 critical code
 var item = List[i];
 // do something with item     • Any idea why?
}
Switch vs If
            Matching     Non-
                                         • A switch statement
                         Matching          compiles to a different set
                                           of instructions and
Switch      15,7 sec     0,0 sec           optimized for fast state-
Statement
                                           machines.
If          20,7 sec     0,1 sec
Statement                                • Because each case within a
                                           switch statement does not
                                           rely on earlier cases, the
                                           compiler is able to re-order
                                           the testing in such a way as
                                           to provide the fastest
                                           execution.
Reference: http://www.blackwasp.co.uk/SpeedTestIfElseSwitch_2.aspx
Exceptions
  for (int i=0; i < 5000000; i++)                              • When an exception is
  {                                                              thrown, your application dies
    try
    {                                                            a little bit
      throw new ApplicationException();
    }                                                          • Never throw exceptions in
    catch (ApplicationException)                                 order to control the flow of
    {
    }                                                            the application
  }
                                                               • However, do not use error
                                                                 codes because of concerns
                                                                 that exceptions might affect
Total time taken: 00:00:42.0312500                               performance negatively
Exceptions per millisecond: 118
                                                               • Consider using TryParse()
                                                                 pattern
Reference: http://www.developerfusion.com/article/5250/exceptions-and-performance-in-net/
Data Structures
“Most software today is very much like an Egyptian pyramid with millions of
 bricks piled on top of each other, with no structural integrity, but just done
                   by brute force and thousands of slaves.”
                                    Alan Kay
Strings
string BadConcatenate(string[] items)
                                         • C# strings are immutable
    string strRet = string.Empty;
    foreach(string item in items)        • Prefer String.Concat() to
    {
      strRet += item;                      String.Format()
    }
    return strRet;                       • StringBuilder is the only
}
                                           way to have mutable
string GoodConcatenate(string[] items)     strings, but still creates
{
  var builder = new StringBuilder();
                                           some garbage.
  foreach(string item in items)
  {
    builder.Append(item);
  }
  return builder.ToString();
}
Collections
• Use the right type of the collection for your work
• Stop using List<T> for everything
• Ask yourself:
   o   Do you need to sort your collection?
   o   Do you need to search your collection?
   o   Do you need to access each element by index?
   o   Do you need a custom collection?
Collections
• Use the right type of the collection for your work
• Stop using List<T> for everything
• Ask yourself:
   o Do you need to sort your collection?
      • List<T> to bind read-only sorted data
      • NameValueCollection for sorting strings
      • SortedList<K,V> presorts while constructing
   o Do you need to search your collection?
      • Use Dictionary<K, V>
   o Do you need to access each element by index?
      • Use List<T>, Dictionary<K,V>, SortedList<K,V>
   o Do you need a custom collection?
      • Ask me, you probably don’t need it.
Arrays
// 2D array of 100 x 100 elements.
for (int a = 0; a < 100; a++)            • Multidimensional Arrays [,]
{
  for (int x = 0; x < 100; x++)
                                           are slow.
  {
    int c = a1[a, x];
                                         • Prefer jagged [][] arrays.
}
  }                                      • Arrays have a static size.
                                           The size of the array
// Jagged array of 100 x 100
elements.                                  remains fixed after initial
for (int a = 0; a < 100; a++)
{
                                           allocation.
  for (int x = 0; x < 100; x++)
  {
    int c = a2[a][x];                       2D array looping: 4571 ms
  }                                         Jagged array looping: 2864 ms [faster]
}




             Reference: http://www.dotnetperls.com/regex-performance
RegEx
static Regex wordRegex = new              • A regular expression is
Regex(@"W+", RegexOptions.Compiled);
                                            essentially a state machine
static void Main()
{                                         • Always use
  string s = "This is a simple /string/
for Regex.";
                                            RegexOptions.Compiled if
  string[] c = wordRegex.Split(s);          you plan to reuse a regular
  foreach (string m in c)
  {                                         expression
    Console.WriteLine(m);
  }                                       • RegexOptions.Compiled
}
                                            takes 10x longer to
                                            startup, but yields 30%
                                            better runtime.
          Reference: http://www.dotnetperls.com/regex-performance
Functions
“Controlling complexity is the essence of computer programming”
                         Brian Kernighan
Inline
                                            • You want the compiler
                                              to inline methods
public class MyClass{
  protected virtual void SomeMethod()       • Mark them as sealed
 { ... }
}                                           • This code ends the chain
public class DerivedClass : MyClass {
                                              of virtual overrides and
  protected override sealed void              makes DerivedClass.
   SomeMethod () { ... }
}                                             SomeMethod a
                                              candidate for inlining
Copying Buffers
int[] arr1 = new int[] { 1, 2, 3, 4, 5 };
int[] arr2 = new int[10];                   • Never copy buffers with a
// Copy the first twenty bytes from
                                              loop, prefer
arr1 to arr2                                  Buffer.BlockCopy() or
Buffer.BlockCopy(arr1, 0, arr2, 0,
 5 * sizeof(int)                              Array.Copy()
);
Recursion
private void RecursiveDir(string currentDir)
{                                                          • Avoid recursion
  foreach (var sin Directory.GetDirectories(dir))
   RecursiveDir(s);                                        • Most of the
  foreach (var file in Directory.GetFiles(dir))
   Console.WriteLine(file);
                                                             recursion can be
}                                                            converted to a tail-
private void IterativeDir(string startingDir)                recursion
{
  Stack stackFrame = new Stack();                          • A tail-recursion is a
  stackFrame.Push(startingDir);
  while (stackFrame.Count > 0)                               simple loop
  {
    var current = (string) stackFrame.Pop();
    foreach (var sin Directory.GetDirectories(current ))
     stackFrame.Push(s);
    foreach (var file in Directory.GetFiles(current))
       Console.WriteLine(file);
  }
}
More Tips (1)
•   LINQ is slow, never use it if you need performance
•   Never use reflection if you need performance
•   Keep IO Buffer Size Between 4KB and 8KB
•   Always use Asynchronous IO (Uses IOCP on Windows)
•   ASP.NET: cache aggressively
•   ASP.NET: use session state only if you need to
•   ASP.NET: remove unnecessary HttpModules
•   Experiment!
More Tips (2)
• Reduce boundary crossings (Unmanaged/Managed, Cross
  Process, Cross AppDomain)
• Prefer single large assemblies
• Never use GC.Collect() unless you know what you are doing
• Do not implement Finalize unless required
Questions?
Thank you for your attention!

More Related Content

What's hot

Memory Management In Python The Basics
Memory Management In Python The BasicsMemory Management In Python The Basics
Memory Management In Python The Basics
Nina Zakharenko
 
JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...
JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...
JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...
Maarten Balliauw
 
The OMR GC talk - Ruby Kaigi 2015
The OMR GC talk - Ruby Kaigi 2015The OMR GC talk - Ruby Kaigi 2015
The OMR GC talk - Ruby Kaigi 2015
craig lehmann
 
Scarab: SAT-based Constraint Programming System in Scala / Scala上で実現された制約プログラ...
Scarab: SAT-based Constraint Programming System in Scala / Scala上で実現された制約プログラ...Scarab: SAT-based Constraint Programming System in Scala / Scala上で実現された制約プログラ...
Scarab: SAT-based Constraint Programming System in Scala / Scala上で実現された制約プログラ...
scalaconfjp
 
Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018
Adam Tomat
 
Optimising code using Span<T>
Optimising code using Span<T>Optimising code using Span<T>
Optimising code using Span<T>
Mirco Vanini
 
Packaging perl (LPW2010)
Packaging perl (LPW2010)Packaging perl (LPW2010)
Packaging perl (LPW2010)
p3castro
 
Survey of Program Transformation Technologies
Survey of Program Transformation TechnologiesSurvey of Program Transformation Technologies
Survey of Program Transformation Technologies
Chunhua Liao
 
Final terraform
Final terraformFinal terraform
Final terraform
Gourav Varma
 

What's hot (9)

Memory Management In Python The Basics
Memory Management In Python The BasicsMemory Management In Python The Basics
Memory Management In Python The Basics
 
JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...
JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...
JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...
 
The OMR GC talk - Ruby Kaigi 2015
The OMR GC talk - Ruby Kaigi 2015The OMR GC talk - Ruby Kaigi 2015
The OMR GC talk - Ruby Kaigi 2015
 
Scarab: SAT-based Constraint Programming System in Scala / Scala上で実現された制約プログラ...
Scarab: SAT-based Constraint Programming System in Scala / Scala上で実現された制約プログラ...Scarab: SAT-based Constraint Programming System in Scala / Scala上で実現された制約プログラ...
Scarab: SAT-based Constraint Programming System in Scala / Scala上で実現された制約プログラ...
 
Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018
 
Optimising code using Span<T>
Optimising code using Span<T>Optimising code using Span<T>
Optimising code using Span<T>
 
Packaging perl (LPW2010)
Packaging perl (LPW2010)Packaging perl (LPW2010)
Packaging perl (LPW2010)
 
Survey of Program Transformation Technologies
Survey of Program Transformation TechnologiesSurvey of Program Transformation Technologies
Survey of Program Transformation Technologies
 
Final terraform
Final terraformFinal terraform
Final terraform
 

Similar to Ahieving Performance C#

Multi core programming 2
Multi core programming 2Multi core programming 2
Multi core programming 2
Robin Aggarwal
 
Memory Management & Garbage Collection
Memory Management & Garbage CollectionMemory Management & Garbage Collection
Memory Management & Garbage Collection
Abhishek Sur
 
Никита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-CНикита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-C
DataArt
 
Multi core programming 1
Multi core programming 1Multi core programming 1
Multi core programming 1
Robin Aggarwal
 
Avoid memory leaks using unit tests - Swift Delhi Meetup - Chapter 15
Avoid memory leaks using unit tests - Swift Delhi Meetup - Chapter 15Avoid memory leaks using unit tests - Swift Delhi Meetup - Chapter 15
Avoid memory leaks using unit tests - Swift Delhi Meetup - Chapter 15
Raunak Talwar
 
Parallel Programming
Parallel ProgrammingParallel Programming
Parallel Programming
Mindfire Solutions
 
Run Cloud Native MySQL NDB Cluster in Kubernetes
Run Cloud Native MySQL NDB Cluster in KubernetesRun Cloud Native MySQL NDB Cluster in Kubernetes
Run Cloud Native MySQL NDB Cluster in Kubernetes
Bernd Ocklin
 
Profiler Guided Java Performance Tuning
Profiler Guided Java Performance TuningProfiler Guided Java Performance Tuning
Profiler Guided Java Performance Tuning
osa_ora
 
CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!
Ortus Solutions, Corp
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part I
Eugene Lazutkin
 
Distributed Tensorflow with Kubernetes - data2day - Jakob Karalus
Distributed Tensorflow with Kubernetes - data2day - Jakob KaralusDistributed Tensorflow with Kubernetes - data2day - Jakob Karalus
Distributed Tensorflow with Kubernetes - data2day - Jakob Karalus
Jakob Karalus
 
Real-Time Streaming with Apache Spark Streaming and Apache Storm
Real-Time Streaming with Apache Spark Streaming and Apache StormReal-Time Streaming with Apache Spark Streaming and Apache Storm
Real-Time Streaming with Apache Spark Streaming and Apache Storm
Davorin Vukelic
 
Cloud Native Compiler
Cloud Native CompilerCloud Native Compiler
Cloud Native Compiler
Simon Ritter
 
Ruby on the JVM
Ruby on the JVMRuby on the JVM
Ruby on the JVM
Kresten Krab Thorup
 
Efficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java ApplicationsEfficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java Applications
pkoza
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
Heartin Jacob
 
Hyperloglog Lightning Talk
Hyperloglog Lightning TalkHyperloglog Lightning Talk
Hyperloglog Lightning Talk
Simon Prickett
 
Ruby performance - The low hanging fruit
Ruby performance - The low hanging fruitRuby performance - The low hanging fruit
Ruby performance - The low hanging fruit
Bruce Werdschinski
 
Groovy concurrency
Groovy concurrencyGroovy concurrency
Groovy concurrency
Alex Miller
 
Design patterns
Design patternsDesign patterns
Design patterns
Alok Guha
 

Similar to Ahieving Performance C# (20)

Multi core programming 2
Multi core programming 2Multi core programming 2
Multi core programming 2
 
Memory Management & Garbage Collection
Memory Management & Garbage CollectionMemory Management & Garbage Collection
Memory Management & Garbage Collection
 
Никита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-CНикита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-C
 
Multi core programming 1
Multi core programming 1Multi core programming 1
Multi core programming 1
 
Avoid memory leaks using unit tests - Swift Delhi Meetup - Chapter 15
Avoid memory leaks using unit tests - Swift Delhi Meetup - Chapter 15Avoid memory leaks using unit tests - Swift Delhi Meetup - Chapter 15
Avoid memory leaks using unit tests - Swift Delhi Meetup - Chapter 15
 
Parallel Programming
Parallel ProgrammingParallel Programming
Parallel Programming
 
Run Cloud Native MySQL NDB Cluster in Kubernetes
Run Cloud Native MySQL NDB Cluster in KubernetesRun Cloud Native MySQL NDB Cluster in Kubernetes
Run Cloud Native MySQL NDB Cluster in Kubernetes
 
Profiler Guided Java Performance Tuning
Profiler Guided Java Performance TuningProfiler Guided Java Performance Tuning
Profiler Guided Java Performance Tuning
 
CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part I
 
Distributed Tensorflow with Kubernetes - data2day - Jakob Karalus
Distributed Tensorflow with Kubernetes - data2day - Jakob KaralusDistributed Tensorflow with Kubernetes - data2day - Jakob Karalus
Distributed Tensorflow with Kubernetes - data2day - Jakob Karalus
 
Real-Time Streaming with Apache Spark Streaming and Apache Storm
Real-Time Streaming with Apache Spark Streaming and Apache StormReal-Time Streaming with Apache Spark Streaming and Apache Storm
Real-Time Streaming with Apache Spark Streaming and Apache Storm
 
Cloud Native Compiler
Cloud Native CompilerCloud Native Compiler
Cloud Native Compiler
 
Ruby on the JVM
Ruby on the JVMRuby on the JVM
Ruby on the JVM
 
Efficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java ApplicationsEfficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java Applications
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
 
Hyperloglog Lightning Talk
Hyperloglog Lightning TalkHyperloglog Lightning Talk
Hyperloglog Lightning Talk
 
Ruby performance - The low hanging fruit
Ruby performance - The low hanging fruitRuby performance - The low hanging fruit
Ruby performance - The low hanging fruit
 
Groovy concurrency
Groovy concurrencyGroovy concurrency
Groovy concurrency
 
Design patterns
Design patternsDesign patterns
Design patterns
 

More from Roman Atachiants

Spike-Engine Flyer
Spike-Engine FlyerSpike-Engine Flyer
Spike-Engine Flyer
Roman Atachiants
 
Geant4 Model Testing Framework: From PAW to ROOT
Geant4 Model Testing Framework:  From PAW to ROOTGeant4 Model Testing Framework:  From PAW to ROOT
Geant4 Model Testing Framework: From PAW to ROOT
Roman Atachiants
 
Report: Test49 Geant4 Monte-Carlo Models Testing Tools
Report: Test49 Geant4 Monte-Carlo Models Testing ToolsReport: Test49 Geant4 Monte-Carlo Models Testing Tools
Report: Test49 Geant4 Monte-Carlo Models Testing Tools
Roman Atachiants
 
Research: Applying Various DSP-Related Techniques for Robust Recognition of A...
Research: Applying Various DSP-Related Techniques for Robust Recognition of A...Research: Applying Various DSP-Related Techniques for Robust Recognition of A...
Research: Applying Various DSP-Related Techniques for Robust Recognition of A...
Roman Atachiants
 
Research: Developing an Interactive Web Information Retrieval and Visualizati...
Research: Developing an Interactive Web Information Retrieval and Visualizati...Research: Developing an Interactive Web Information Retrieval and Visualizati...
Research: Developing an Interactive Web Information Retrieval and Visualizati...
Roman Atachiants
 
B.Sc Thesis: Moteur 3D en XNA pour un simulateur de vol
B.Sc Thesis: Moteur 3D en XNA pour un simulateur de volB.Sc Thesis: Moteur 3D en XNA pour un simulateur de vol
B.Sc Thesis: Moteur 3D en XNA pour un simulateur de vol
Roman Atachiants
 
Master Thesis: The Design of a Rich Internet Application for Exploratory Sear...
Master Thesis: The Design of a Rich Internet Application for Exploratory Sear...Master Thesis: The Design of a Rich Internet Application for Exploratory Sear...
Master Thesis: The Design of a Rich Internet Application for Exploratory Sear...
Roman Atachiants
 

More from Roman Atachiants (7)

Spike-Engine Flyer
Spike-Engine FlyerSpike-Engine Flyer
Spike-Engine Flyer
 
Geant4 Model Testing Framework: From PAW to ROOT
Geant4 Model Testing Framework:  From PAW to ROOTGeant4 Model Testing Framework:  From PAW to ROOT
Geant4 Model Testing Framework: From PAW to ROOT
 
Report: Test49 Geant4 Monte-Carlo Models Testing Tools
Report: Test49 Geant4 Monte-Carlo Models Testing ToolsReport: Test49 Geant4 Monte-Carlo Models Testing Tools
Report: Test49 Geant4 Monte-Carlo Models Testing Tools
 
Research: Applying Various DSP-Related Techniques for Robust Recognition of A...
Research: Applying Various DSP-Related Techniques for Robust Recognition of A...Research: Applying Various DSP-Related Techniques for Robust Recognition of A...
Research: Applying Various DSP-Related Techniques for Robust Recognition of A...
 
Research: Developing an Interactive Web Information Retrieval and Visualizati...
Research: Developing an Interactive Web Information Retrieval and Visualizati...Research: Developing an Interactive Web Information Retrieval and Visualizati...
Research: Developing an Interactive Web Information Retrieval and Visualizati...
 
B.Sc Thesis: Moteur 3D en XNA pour un simulateur de vol
B.Sc Thesis: Moteur 3D en XNA pour un simulateur de volB.Sc Thesis: Moteur 3D en XNA pour un simulateur de vol
B.Sc Thesis: Moteur 3D en XNA pour un simulateur de vol
 
Master Thesis: The Design of a Rich Internet Application for Exploratory Sear...
Master Thesis: The Design of a Rich Internet Application for Exploratory Sear...Master Thesis: The Design of a Rich Internet Application for Exploratory Sear...
Master Thesis: The Design of a Rich Internet Application for Exploratory Sear...
 

Recently uploaded

Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
TIPNGVN2
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Zilliz
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
Rohit Gautam
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 

Recently uploaded (20)

Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 

Ahieving Performance C#

  • 1. Achieving Performance in C# Roman Atachiants
  • 2. Contents • Introduction • Memory Management • Flow and Control Structures • Data Structures • Function Calls • Questions
  • 3. Introduction “Premature optimization is the root of all evil” Prof. Donald Knuth
  • 5. When & Where • One should not optimize everything prematurely • One should keep in mind good programming practices • One should evaluate the
  • 6. The Secret Not to do ANY work.
  • 7. Memory Management “The memory management on PowerPC can be used to frighten small children” Linus Torvalds
  • 8. GC • The .NET Framework uses automatic garbage collection to manage memory for all applications. Memory for an Object’s object is memory is freed allocated from (collected) the managed some time later heap when you call new Object dies tu to all its references Object either being constructor is explicitly set to called null or going out of scope Object is used for some time
  • 9. Assemblies • Prefer single large assemblies rather than multiple smaller assemblies • Overhead: o The cost of loading metadata for smaller assemblies o Touching various memory pages in pre-compiled images in the CLR in order to load the assembly (Ngen) o JIT compile time o Security checks • Sometimes you cannot avoid splitting assemblies; for example, for versioning and deployment reasons. If you need to ship types separately, you may need separate assemblies.
  • 10. Memory • Allocation: o Is super fast! • Free: o Is super slow! • You want to avoid freeing memory. The easiest way to do so is not to allocate in the first place.
  • 11. Reuse Memory • Do not use a new in a loop for (int i = 0; i < 100; ++i) unless you really need to. { var numbers = new int[10]; // (...) Do something with numbers Console.WriteLine(numbers.Sum()); } var numbers = new int[10]; for (int i = 0; i < 100; ++i) { // (...) Do something with numbers Console.WriteLine(numbers.Sum()); }
  • 12. Object Pools public sealed class ScriptBuilder : RecyclableObject • Object Pooling is { ... something that tries to } keep a pool of objects in public sealed class ScriptBuilderPool : memory to be re-used ConcurrentPool<ScriptBuilder> { later and hence it will public static readonly ScriptBuilderPool Default = new ScriptBuilderPool(); reduce the load of object public ScriptBuilderPool() : creation to a great extent base("ScriptBuilders", _ => new ScriptBuilder()){ } } • Object Pool is nothing but using (var obj = ScriptBuilderPool.Default.Acquire()) a container of objects { // Do stuff that are ready for use }
  • 13. Class Design • Do not make classes thread safe by default. • Consider using the sealed keyword. • Consider the tradeoffs of virtual members. • Consider using overloaded methods. • Consider overriding the Equals method for value types. • Know the cost of accessing a property. • Consider private vs. public member variables. • Limit the use of volatile fields.
  • 14. Class vs Struct • Class o A reference type o Lives in the heap o Slower • Struct o A value type o Lives in the stack o Faster • … But, never use structs that are bigger than 16 bytes. Use them wisely.
  • 15. Threads • Thread threads as a shared resource o Do not create threads on a per-request basis because this can severely impact scalability. Creating new threads is also a fairly expensive operation that should be minimized. Treat threads as a shared resource and use the optimized .NET thread pool. • Locking is slow, avoid locking large portions of code • Minimize thread creation • Use the thread pool when you need threads • Use a timer to schedule periodic tasks • Never use Thread.Abort • Never use Thread.Suspend or Thread.Resume
  • 16. Flow & Control “It is practically impossible to teach good programming style to students that have had prior exposure to BASIC. As potential programmers, they are mentally mutilated beyond hope of regeneration.” E. W. Dijkstra
  • 17. For vs For..Each • For and for .. each loops foreach(var item in List) are different: { o For each is less performant than a // do something with item for loop } o For each creates garbage int count = List.Length; • Always prefer for loops for for(int i=0; i < count; ++i) { critical code var item = List[i]; // do something with item • Any idea why? }
  • 18. Switch vs If Matching Non- • A switch statement Matching compiles to a different set of instructions and Switch 15,7 sec 0,0 sec optimized for fast state- Statement machines. If 20,7 sec 0,1 sec Statement • Because each case within a switch statement does not rely on earlier cases, the compiler is able to re-order the testing in such a way as to provide the fastest execution. Reference: http://www.blackwasp.co.uk/SpeedTestIfElseSwitch_2.aspx
  • 19. Exceptions for (int i=0; i < 5000000; i++) • When an exception is { thrown, your application dies try { a little bit throw new ApplicationException(); } • Never throw exceptions in catch (ApplicationException) order to control the flow of { } the application } • However, do not use error codes because of concerns that exceptions might affect Total time taken: 00:00:42.0312500 performance negatively Exceptions per millisecond: 118 • Consider using TryParse() pattern Reference: http://www.developerfusion.com/article/5250/exceptions-and-performance-in-net/
  • 20. Data Structures “Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves.” Alan Kay
  • 21. Strings string BadConcatenate(string[] items) • C# strings are immutable string strRet = string.Empty; foreach(string item in items) • Prefer String.Concat() to { strRet += item; String.Format() } return strRet; • StringBuilder is the only } way to have mutable string GoodConcatenate(string[] items) strings, but still creates { var builder = new StringBuilder(); some garbage. foreach(string item in items) { builder.Append(item); } return builder.ToString(); }
  • 22. Collections • Use the right type of the collection for your work • Stop using List<T> for everything • Ask yourself: o Do you need to sort your collection? o Do you need to search your collection? o Do you need to access each element by index? o Do you need a custom collection?
  • 23. Collections • Use the right type of the collection for your work • Stop using List<T> for everything • Ask yourself: o Do you need to sort your collection? • List<T> to bind read-only sorted data • NameValueCollection for sorting strings • SortedList<K,V> presorts while constructing o Do you need to search your collection? • Use Dictionary<K, V> o Do you need to access each element by index? • Use List<T>, Dictionary<K,V>, SortedList<K,V> o Do you need a custom collection? • Ask me, you probably don’t need it.
  • 24. Arrays // 2D array of 100 x 100 elements. for (int a = 0; a < 100; a++) • Multidimensional Arrays [,] { for (int x = 0; x < 100; x++) are slow. { int c = a1[a, x]; • Prefer jagged [][] arrays. } } • Arrays have a static size. The size of the array // Jagged array of 100 x 100 elements. remains fixed after initial for (int a = 0; a < 100; a++) { allocation. for (int x = 0; x < 100; x++) { int c = a2[a][x]; 2D array looping: 4571 ms } Jagged array looping: 2864 ms [faster] } Reference: http://www.dotnetperls.com/regex-performance
  • 25. RegEx static Regex wordRegex = new • A regular expression is Regex(@"W+", RegexOptions.Compiled); essentially a state machine static void Main() { • Always use string s = "This is a simple /string/ for Regex."; RegexOptions.Compiled if string[] c = wordRegex.Split(s); you plan to reuse a regular foreach (string m in c) { expression Console.WriteLine(m); } • RegexOptions.Compiled } takes 10x longer to startup, but yields 30% better runtime. Reference: http://www.dotnetperls.com/regex-performance
  • 26. Functions “Controlling complexity is the essence of computer programming” Brian Kernighan
  • 27. Inline • You want the compiler to inline methods public class MyClass{ protected virtual void SomeMethod() • Mark them as sealed { ... } } • This code ends the chain public class DerivedClass : MyClass { of virtual overrides and protected override sealed void makes DerivedClass. SomeMethod () { ... } } SomeMethod a candidate for inlining
  • 28. Copying Buffers int[] arr1 = new int[] { 1, 2, 3, 4, 5 }; int[] arr2 = new int[10]; • Never copy buffers with a // Copy the first twenty bytes from loop, prefer arr1 to arr2 Buffer.BlockCopy() or Buffer.BlockCopy(arr1, 0, arr2, 0, 5 * sizeof(int) Array.Copy() );
  • 29. Recursion private void RecursiveDir(string currentDir) { • Avoid recursion foreach (var sin Directory.GetDirectories(dir)) RecursiveDir(s); • Most of the foreach (var file in Directory.GetFiles(dir)) Console.WriteLine(file); recursion can be } converted to a tail- private void IterativeDir(string startingDir) recursion { Stack stackFrame = new Stack(); • A tail-recursion is a stackFrame.Push(startingDir); while (stackFrame.Count > 0) simple loop { var current = (string) stackFrame.Pop(); foreach (var sin Directory.GetDirectories(current )) stackFrame.Push(s); foreach (var file in Directory.GetFiles(current)) Console.WriteLine(file); } }
  • 30. More Tips (1) • LINQ is slow, never use it if you need performance • Never use reflection if you need performance • Keep IO Buffer Size Between 4KB and 8KB • Always use Asynchronous IO (Uses IOCP on Windows) • ASP.NET: cache aggressively • ASP.NET: use session state only if you need to • ASP.NET: remove unnecessary HttpModules • Experiment!
  • 31. More Tips (2) • Reduce boundary crossings (Unmanaged/Managed, Cross Process, Cross AppDomain) • Prefer single large assemblies • Never use GC.Collect() unless you know what you are doing • Do not implement Finalize unless required
  • 32. Questions? Thank you for your attention!