Code Optimization Technique in C# – Who Cares about Performance?

0
86

Code Optimization Technique in C#

Readers of the Topic – Developers

Coverage Topic

  • string.empty vs. double quotes (“”)
  • string.Equals vs. ==
  • string.Intern
  • Strings vs. StringBuilder
  • When to choose StringBuilder
  • String concatenation vs. string.Format
  • Advantages of string.Format
  • Case insensitive comparisons
  • Out vs. return
  • Remove un-necessary Blank Lines
  • Linq – First vs. FirstOrDefault
  • When to use FirstOrDefault
  • Single vs. SingleOrDefault
  • Performance: FirstOrDefault vs. SingleOrDefault
  • IDisposable.Dispose vs. Using Block

Let’s Drill Down the Basic Concept

Better to follow some guidelines,

  • It will remove extra headache.
  • It will improve code readability.
  • It will improve code performance.
  • Better communication programmer to programmer.

string.empty vs. double quotes (“”)

  • Double quotes create an object.
  • String.empty doesn’t create an object.

Performance: string.empty is faster than double quotes (“”).

EmptyString

string.Equals vs. ==

Equals

  • ==” compares object references; calls at compile time;
  • ‘string.Equals’ compares values; calls at runtime;

Performance: string.Equals is faster than ==

Few More Points:

  • string.Equals and “==” are same for primitive data types or value types (int, double, char, bool);Because it is content base comparison.
  • String is non-primitive; but string.Equals and “==” are similar for it. It takes content base comparison. If the contents of the two string variables are same, then they are pointing to the same memory location and value.
  • If one object or both are null, then string.Equals will throw NullReferenceException; but == will not throw exception.

string.Intern

If we need to copy same value of the string again and again, then better to use string.Intern to improve the performance.

StringInternExample

Strings vs. StringBuilder

  • The term immutable mean: The data value may not be changed. 

The values of the variable may be changed, but the original immutable values will be discarded and a new data value will be created in memory.

  • Strings are immutable and StringBuilder is mutable.

StringAndStringBuilder

  • If you need large amount of string manipulation, use StringBuilder.
  • Strings are immutable. So, if you change your strings, then every time a new instance will be created into the memory.

When to choose StringBuilder

  • If you need more than THREE times string manipulating, use StringBuilder.

String concatenation vs. string.Format

StringConCatStringFormat

  • Do use AppendFormat for StringBuilder.
  • Do use String.format for string.

Advantages

  • Improve readability.
  • Improve performance.
  • Easy to modify.

Case insensitive comparisons

CaseIgnoreCompare

Out vs. return

Performance: The method that use return, is faster than out.

OutReturn

Remove Un-necessary Blank Lines

BlankLine

  • Better to remove un-necessary blank lines and white spaces.
  • It improves code readability.

Linq – First vs. FirstOrDefault

LinqFirstOrDefault

  • First: There is one or more than one element in the result, but you need first one or empty is not okay. Throws exception, if list is empty;
  • FirstOrDefault: There is one or more than one element in the result, but you need first one or empty is okay. It throws exception, if source-list is null.

When to use FirstOrDefault

  • When more than one results are expected and you need the first record.
  • Or if there is no result; but you want default value.
  • No exception, if result is empty.

Single vs. SingleOrDefault

  • single:If you need only 1 element(not more than 1 or zero).
  • SingleOrDefault:If you need only 1 or zero element(not more than 1).

Performance: FirstOrDefault vs. SingleOrDefault

FirstOrDefault is faster than SingleOrDefault.

IDisposable.Dispose vs. Using Block

Better to use ‘Using block’. It calls dispose, although there could have an exception or return statement.

One More Point:

  • Don’t use magic value (hard coded string, number) directly in your code block. Place all of the hard coded values into one separate class or at least on top of the class. It will improve code manageability.

HardCodedValue

Still I want to say more but I’m used to caffeine-driven development. I have already finished my coffee. So, no more words. If you maintain good practices, you are ready to move on to the next journey. Don’t let you down.

LEAVE A REPLY