Created
July 18, 2018 18:28
-
-
Save nicknow/4f064c04b386f9bacb71a45666c7a2e5 to your computer and use it in GitHub Desktop.
Test to determine if there is a performance impact calling a method passed as an Action instead of directly. To be run in LinqPad (as a C# Program)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
RunDirectly(); | |
RunIndirectly(); | |
var sw = new Stopwatch(); | |
sw.Start(); | |
for (int i = 0; i < 100; i++) | |
{ | |
RunDirectly(); | |
} | |
sw.Stop(); | |
Console.ForegroundColor = ConsoleColor.Green; | |
Console.WriteLine($"100 Direct Executions: {sw.ElapsedMilliseconds}"); | |
sw.Reset(); | |
sw.Start(); | |
for (int i = 0; i < 100; i++) | |
{ | |
RunIndirectly(); | |
} | |
sw.Stop(); | |
Console.ForegroundColor = ConsoleColor.Blue; | |
Console.WriteLine($"100 Indirect Executions: {sw.ElapsedMilliseconds}"); | |
sw.Reset(); | |
sw.Start(); | |
for (int i = 0; i < 100; i++) | |
{ | |
RunIndirectly(); | |
} | |
sw.Stop(); | |
Console.ForegroundColor = ConsoleColor.Blue; | |
Console.WriteLine($"100 Indirect Executions: {sw.ElapsedMilliseconds}"); | |
sw.Reset(); | |
sw.Start(); | |
for (int i = 0; i < 100; i++) | |
{ | |
RunDirectly(); | |
} | |
sw.Stop(); | |
Console.ForegroundColor = ConsoleColor.Green; | |
Console.WriteLine($"100 Direct Executions: {sw.ElapsedMilliseconds}"); | |
sw.Reset(); | |
sw.Start(); | |
for (int i = 0; i < 100; i++) | |
{ | |
RunDirectly(); | |
} | |
sw.Stop(); | |
Console.ForegroundColor = ConsoleColor.Green; | |
Console.WriteLine($"100 Direct Executions: {sw.ElapsedMilliseconds}"); | |
sw.Reset(); | |
sw.Start(); | |
for (int i = 0; i < 100; i++) | |
{ | |
RunIndirectly(); | |
} | |
sw.Stop(); | |
Console.ForegroundColor = ConsoleColor.Blue; | |
Console.WriteLine($"100 Indirect Executions: {sw.ElapsedMilliseconds}"); | |
sw.Reset(); | |
sw.Start(); | |
for (int i = 0; i < 100; i++) | |
{ | |
RunIndirectly(); | |
} | |
sw.Stop(); | |
Console.ForegroundColor = ConsoleColor.Blue; | |
Console.WriteLine($"100 Indirect Executions: {sw.ElapsedMilliseconds}"); | |
sw.Reset(); | |
sw.Start(); | |
for (int i = 0; i < 100; i++) | |
{ | |
RunDirectly(); | |
} | |
sw.Stop(); | |
Console.ForegroundColor = ConsoleColor.Green; | |
Console.WriteLine($"100 Direct Executions: {sw.ElapsedMilliseconds}"); | |
sw.Reset(); | |
} | |
private void RunDirectly() | |
{ | |
var logger = new Logger(); | |
try | |
{ | |
SomeMethod(); | |
logger.Write("Completed Without Exception"); | |
return; | |
} | |
catch (Exception ex) | |
{ | |
logger.Write("Exception During Crm Service Operation"); | |
logger.Write(ex); | |
throw; | |
} | |
} | |
private void RunIndirectly() | |
{ | |
var logger = new Logger(); | |
Run(SomeMethod,logger); | |
} | |
private void SomeMethod() | |
{ | |
SomeMethod(20); | |
} | |
private void SomeMethod(int milliSeconds) | |
{ | |
Thread.Sleep(milliSeconds); | |
} | |
private void Run(Action method, Logger _logger) | |
{ | |
Run<object>(() => | |
{ | |
method(); | |
return null; | |
}, _logger); | |
} | |
// Define other methods and classes here | |
private T Run<T>(Func<T> method, Logger _logger) | |
{ | |
if (method == null) | |
{ | |
_logger.Write($"Null Argument: {nameof(method)}"); | |
throw new ArgumentNullException(nameof(method)); | |
} | |
try | |
{ | |
var result = method(); | |
return _logger.WriteAndReturn(result, "Completed Without Exception"); | |
} | |
catch (Exception ex) | |
{ | |
_logger.Write("Exception During Crm Service Operation"); | |
_logger.Write(ex); | |
throw; | |
} | |
} | |
public class Logger | |
{ | |
public T WriteAndReturn<T>(T returnObject, string contents) | |
{ | |
return returnObject; | |
} | |
public void Write(string contents) | |
{} | |
public void Write(Exception contents) | |
{} | |
} |
I would consider that a negligible difference.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here are the results I get when running this:
100 Direct Executions: 3268
100 Indirect Executions: 3203
100 Indirect Executions: 3241
100 Direct Executions: 3234
100 Direct Executions: 3272
100 Indirect Executions: 3234
100 Indirect Executions: 3271
100 Direct Executions: 3301