Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the most important optimizing performance best practices in C# [closed]

When I was reading this tutorial I noticed the following performance tip about using structs in C#:

Unless you need reference type semantics, a class that is smaller than 16 bytes may be more efficiently handled by the system as a struct.

I looked for similar question in stackoverflow and I found some questions that talk about performance best practices in ADO.Net, Networking, Streams, but not about performance best practices in C# (The language).

I want to add another tip about using the integer types:

The runtime optimizes the performance of 32-bit integer types (Int32 and UInt32), so use those types for counters and other frequently accessed integral variables.

like image 799
Homam Avatar asked Oct 17 '10 22:10

Homam


2 Answers

Simply: profile.

Every app is different. Taking time to reduce some code to make it "more efficient" is meaningless if that is not a bottleneck in you app. Also - you may even be making things worse if you don't have numbers to support changes.

In most cases IO is the pinch-point, so thinking about IO is a no-brainer. Ditto DB access. But beyond that: measure it.

like image 172
Marc Gravell Avatar answered Nov 11 '22 14:11

Marc Gravell


  1. Strings are Immutable.
  2. Understand the using statement.
  3. Understand Boxing and how Generics help.
  4. Understand how the Garbage Collector works.
  5. Parallel programming in .Net 4.0
  6. Understand how File IO affects performance.

Eric Lippert talks alot about optimization. I would read his blog.
I would check out Jon Skeet's blog also.

like image 12
kemiller2002 Avatar answered Nov 11 '22 15:11

kemiller2002