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# Inline Optimization

Inline statements for speed. Compare ways of inlining statements in this language.
Inline optimization. Manually inlining a method body affects performance. Until recently, C# did not provide macros or the inline keyword. But you can always paste in nested function bodies into the enclosing methods. This influences performance.Optimization

Caution: Please consider the AggressiveInlining option built into the .NET Framework before using this approach.

AggressiveInlining
Example. In this benchmark program, we test the execution engine performance of the .NET Framework on two different methods: A and B. Method A contains all the necessary operations in its body directly.Benchmark

And: Method B contains another method call (C) that does part of the execution logic.

Then: Main demonstrates the performance difference between the two first methods.

Info: It shows that manually inlining improves performance by about 1.5 nanoseconds per invocation on the test computer.

C# program that benchmarks inlined method using System; using System.Diagnostics; class Program { static int A(int v) { // This is a single method call. // ... It contains twenty increments. v++; v++; v++; v++; v++; v++; v++; v++; v++; v++; v++; v++; v++; v++; v++; v++; v++; v++; v++; v++; return v; } static int B(int v) { // This does ten increments. // ... Then it does ten more increments in another method. v++; v++; v++; v++; v++; v++; v++; v++; v++; v++; v = C(v); return v; } static int C(int v) { // This does ten increments. v++; v++; v++; v++; v++; v++; v++; v++; v++; v++; return v; } static void Main() { const int max = 100000000; int temp1 = 0; int temp2 = 0; A(0); B(0); C(0); var s1 = Stopwatch.StartNew(); for (int i = 0; i < max; i++) { temp1 = A(i); } s1.Stop(); var s2 = Stopwatch.StartNew(); for (int i = 0; i < max; i++) { temp2 = B(i); } s2.Stop(); Console.WriteLine(s1.Elapsed.TotalMilliseconds * 1000 * 1000 / (double)max); Console.WriteLine(s2.Elapsed.TotalMilliseconds * 1000 * 1000 / (double)max); Console.WriteLine("{0} {1}", temp1, temp2); Console.Read(); } } Output 2.236963 (inlined) 3.820417 (regular method) 100000019 100000019
Discussion. In structural programming, developers use routines to organize complex programs. However, computers execute instructions, not routines. Compilers translate methods into series of instructions that operate on the evaluation stack.

And: Unfortunately, these method instructions are pure overhead, at least when they are not required for modularity of design.

Tip: This explains the benefit to inlining methods. The just-in-time compiler inlines certain methods, but typically only small ones.

The AggressiveInlining option is available in .NET 4.5. Microsoft works continually to make C# faster. And this continually invalidates my articles. With AggressiveInlining, you can inline methods.
Code Complete. The book Code Complete by Steve McConnell discusses the inline routine optimization. It states that in some languages, such as C++ and C, you can use the inline keyword or macros with #define.
The benchmark in that book shows that inlining a routine in C++ was mildly beneficial, but not beneficial in another language. This optimization makes some programs slower and others faster. You must test it to be certain of its effect.
Summary. We explored inlining methods using a manual optimization technique in the C# language. This technique can explode the complexity of your program. But it can also improve performance in a measurable way.
© TheDeveloperBlog.com
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