c# - Action faster than method call? -


Some time ago I came to know about a method that the action is faster than the pure method call. I do not remember where it is, but had to move in the moment, did some tests and found that it tells the truth in some cases.

Someone today says that what I was doing was wrong, so anyone can get a reason, in these tests, the work of the parameters is faster than pure methods? I must say that this is only faster in x64 machines, slow in x86.

  class program {static zero main (string [] args) {test t = new test (); T.doTest (); Console.ReadKey (); }} Class test {stopwatch w; int a = 0; Public zero doTest () {action doSum = () = & gt; {A = A + 1; }; & Lt; Integer & gt; DoSumValue = (plus) = & gt; {A = A + add; }; w = new stopwatch (); Console WrightLine ("------- with params -------"); {A = 0; for intented = 0; round and lieutenant; 10; round ++) W.Reset (); w.Start (); For (Intubsee = 0; Books & lt; 500000000; buc ++) Sum (1); w.Stop (); Console.light line ("function:" + w. Applied milliseconds); A = 0; W.Reset (); w.Start (); For (Intubsee = 0; Booky <50,0000000; buc ++) doSumValue (1); w.Stop (); Console.light line ("Action:" + w.ElapsedMilliseconds); } Console. WrightLine ("-------- Without a parameter ---------"); {A = 0; for intented = 0; round and lieutenant; 10; round ++) W.Reset (); W.Start (); For (intubic = 0; bouquet; lt; 500000000; buc ++) yoga (); w.Stop (); Console.light line ("function:" + w. Applied milliseconds); A = 0; W.Reset (); w.Start (); For (Intubsee = 0; Booke> 50,0000000; buc ++) doSum (); w.Stop (); Console.light line ("Action:" + w.ElapsedMilliseconds); }} Zero sum () {a = a + 1; } Zero sum value (atad) {a = a + toAdd; }}   

Edit: changes to the loop count for surpacing 1 second with the same result

Results on my machine (I7 2600) are:

New results with many details

with -x86 * parameter

Function: 1417ms Action: 1568ms

* Without parameters

Function: 1422ms Action: 1634ms

- x64 * with parameter

Function: 1 9 76ms Action: 2114ms

* Without parameters

Function: 1975ms Action: 1719ms

Less text C Old results with OUNT

with -x86 parameter

Function: 28ms

Action: 31ms

without -x86 parameter

function: 28ms

Task: 32ms

-x64 parameter

Function: 39ms

Action: 42ms

with -x64 parameter < P>

Function: 39ms

Action: 34ms

So, whether my test is incorrect or the parameters of parameter parameters in x64 machines Are faster work than calls?

verb {/ code> is a representative, for all purposes and purposes in NAT , There is only one method indicator. That is, it is an indicator for a method, so logically it makes the same basic instructions and calls a method under cover. There is no reason to normal the reason that the action written by the action action will be faster than the call. There are many scenarios where it can be a little slow as it is the level of intuition around a method call - but not in a meaningful way.

Comments

Popular posts from this blog

jasper reports - How to center align barcode using jasperreports and barcode4j -

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -