TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Optimization

Apply low-level optimizations to speed up C# programs. Tune program performance.
Optimization. Deep in the ocean an octopus hides from predators. It disguises itself on the ocean floor. When attacked, it releases dark ink.
For an octopus, survival depends on escape. It optimizes to avoid being eaten. A C# program too can be optimized for performance, to work better and faster.
Benchmark. We must take measurements. Use Stopwatch and your mission will become easier. Keep track of the numbers in a text file (the octopus lacks this advantage).BenchmarkBenchmark: memory hierarchyBenchmark: ProfileOptimizationBenchmark: use Stopwatch
List optimizations. The List is a beautiful thing. But if you use it foolishly, your program ends up in bad shape. A predator (like a shark) may eat it before it finishes.Lists: avoid lookupsLists: count fastLists: prefer for-loopLists: remove fastLists: use capacityLists: use Clear
Memory optimizations. Memory is a physical thing. It has shape and size (like an atom). In our performance quest we should conserve memory—use as little as possible.Memory: locality of referenceMemory: measure class sizeMemory: temporal localityMemory: use arraysMemory: use bit masksMemory: use BitArray
Array optimizations. Arrays store elements in a fast way. As the octopus hides, it could store its location in a compact array. This would help it escape faster.Array: cacheArray: count fastArray: flatten 2D arrayArray: use Buffer.BlockCopyArray: use byte arrayArray: use jagged arrayArray: use lookup tableArray: use sentinel elementArray: use separate arrays
Regex optimizations. In the deepest part of the ocean, we find regular expressions. But even Regex can be optimized. Often we optimize a Regex by removing it.Regex: optimizeRegex: prefer string loopRegex: use Compiled
Class optimizations. Prefer classes to structs. When we pass a class to a method, only a reference is copied. With classes we avoid moving things in memory.Classes: avoid structsClasses: cache DateTimeClasses: cache typeofClasses: use static fieldsClasses: use structsClasses: use treeClasses: use Tuple
Branch optimizations. For survival the octopus does not spend its time evaluating slow logic. It moves quickly. For C# we speed up our branches with switches or for-loops.Statements: avoid LINQStatements: avoid yieldStatements: hoist try, catchStatements: reorder ifStatements: unwinding, jamming, decrementStatements: use complex expressionsStatements: use switch
Some research. You can find many optimization tips in Code Complete. I recommend this book for some tips. Some strategies may not be useful in C#. Many are still relevant.

Quote: Use a high-quality design. Make the program right. Make it modular and easily modifiable so that it's easy to work on later.

Quote: When it's complete and correct, check the performance. If the program lumbers, make it fast and small. Don't optimize until you know you need to (Code Complete).

A final note. As an octopus, hiding on the ocean floor is something to optimize for. In C# programs, optimizations to reduce allocations and reduce logical tests are more effective.
© TheDeveloperBlog.com
Home
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf