In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use inline functions in C#. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
There is an inline function in C++ that uses inline to modify the function, and the compiler optimizes it and inserts the function into the call as a code judgment.
When a function call is executed, it is necessary to first allocate storage space for formal parameters and local variables in the stack, then copy the value of the argument to the formal parameter, and then put the return address of the function (which indicates where the program should go back to continue execution) on the stack, and finally jump to the internal execution of the function. This process takes time.
In addition, when the function returns from the return statement, it needs to recover the storage space occupied by formal parameters and local variables from the stack, then take the return address from the stack, and then jump to that address to continue execution, which will also take time.
In C #, you can tell the compiler to optimize it by using features on the function to achieve the same goal.
[MethodImpl (MethodImplOptions.AggressiveInlining)]
Examples are as follows:
Using System;using System.Diagnostics;using System.Runtime.CompilerServices;class Program {const int _ max = 100000000; static void Main () {int sum = 0; Stopwatch S1 = Stopwatch.StartNew (); for (int I = 0; I < _ max; iTunes +) {sum + = Method1 ();} s1.Stop (); Stopwatch S2 = Stopwatch.StartNew () For (int I = 0; I < _ max; iTunes +) {sum + = Method2 ();} s2.Stop (); Console.WriteLine (double) (s1.Elapsed.TotalMilliseconds * 1000000) / _ max) .ToString ("0.00 ns")) Console.WriteLine ((double) (s2.Elapsed.TotalMilliseconds * 1000000) / _ max) .ToString ("0.00 ns")); Console.Read () } static int Method1 () {return "one" .length + "two" .length + "three" .length + "four" .length + "five" .length + "six" .length + "seven" .length + "eight" .length + "nine" .length + "ten" .length } [MethodImpl (MethodImplOptions.AggressiveInlining)] static int Method2 () {return "one" .length + "two" .length + "three" .length + "four" .length + "five" .length + "six" .length + "seven" .Le ngth + "eight" .length + "nine" .length + "ten" .length;}}
Test results:
21.92 ns3.22 ns about "how to use inline functions in C#" is shared here. I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.