Geeks With Blogs
Abhishek Anand Bits & Bytes about .net technology framework.
Using the “static” keyword
  • Static classes can only contain static members and cannot be instantiated. So using the static keyword will make your code a bit faster since no object creation is involved.
  • Static methods can be accessed without an instance of the class being created.
  • Static fields are shared across all instances of that class.
  • In-short, they are all loaded once into memory and do not require instance resolution at run-time, which reduces the amount of instructions needed to call the method.
  • Static classes are sealed by default, which means they cannot be inherited further. This is because static classes have no behavior at all.
  • Important distinction between const and readonly keywords in C#: const variables are implicitly static and they need to be defined when declared. readonly variables are not implicitly static and can only be initialized once.

Avoiding unneeded local variable and parameters

  • Whenever possible, minimize the amount of parameters being passed to a method
    • If you need to pass in more parameters, consider using a class that contains all of the parameters.
  • Try to separate local variables into fields when possible so that the memory is only allocated then the object is created and not each time the method is called.
    • This has a tradeoff of the variable having a longer lifespan due to it not being marked for garbage collection on exit of the method
    • Also this depends on the situation, so be sure to try both ways to see which is better in the given context
Utilizing Constants
  • No memory is ever allocated for constants. Instead their values are directly injected by the compiler into the instructions sent to the CPU
  • Whenever possible, mark fields as constants using the const or readonly keywords

Using Switch vs. If blocks

  • Switch statements are usually compiled into a “jump table” (essentially a series of “goto” statements) which is much more efficient is most cases than if-else if blocks.
    • Only applies to integer and enumeration types
  • When possible, use a switch statement instead of a series of if-else ifs

Using For vs. ForEach

  • It is often more efficient for a loop to decrement it’s counter and check against zero, rather than increment
  • The reason for this speed up is that modern processors have optimized checking a value against zero with special instructions which run faster than checking against other values
  •  When possible, decrement a loop counter in a “for” loop instead of increment
  • Note: Will only increase efficiency by around 1 millisecond for most programs, unless extremely large loops executing (1 ms for every 1 million loop iterations)

Avoiding Exceptions

  • Throwing exceptions in .NET can be a very expensive operation since exceptions come with lot of necessary overhead and interrupt the current flow of execution.
    • Also, stay away from catching exceptions and re-throwing them with additional information
  • “try/catch” blocks should always be at the top most level possible for the situation
    • For example if you are iterating in a loop and the “try/catch” is inside the loop, placing the exception handling outside/around the loop will yield a significant performance increase
  •  If possible, try to reduce the number of exceptions being thrown all together.
  • If possible, place the exception handling logic as high in the application stack as possible.
Using Preprocessor Directives
  • Just like C#’s ancestors (C++ and C), the language comes with the ability to conditionally compile parts of a file. This is accomplished by using preprocessor directives
  • Applications can use preprocessor directives to add code based on some condition.
Posted on Monday, October 21, 2013 2:52 PM .net , performance | Back to top


Comments on this post: Performance Gains

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Abhishek Anand | Powered by: GeeksWithBlogs.net