Advertisement
Guest User

Method vs Delegate vs Reflection Calls

a guest
Jul 20th, 2016
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Collections.ObjectModel;
  5. using System.Diagnostics;
  6.  
  7. public class Program
  8. {
  9.     public static void Main()
  10.     {
  11.         TimeSpan amount1 = TimeSpan.Zero, amount2 = TimeSpan.Zero, amount3 = TimeSpan.Zero;
  12.         var cnt = 0;
  13.         var s1 = new Stopwatch();
  14.         var s2 = new Stopwatch();
  15.         var s3 = new Stopwatch();
  16.         const string str = "100";
  17.         int i;
  18.         var parser2 = ExpressionBuilder.StructParsers[typeof(int)];
  19.         object o2;
  20.         var parser = ParserHelper.StructParsers[typeof(int)];
  21.         object o;
  22.         while( true )
  23.         {
  24.             //Method
  25.             s1.Start();
  26.             for( var j = 0; j < 10000000; j++ )
  27.             {
  28.                 int.TryParse( str, out i );
  29.  
  30.             }
  31.             s1.Stop();
  32.  
  33.             //Delegate
  34.             s2.Start();
  35.             for( var j = 0; j < 10000000; j++ )
  36.                 parser2( str, out o2 );
  37.  
  38.             s2.Stop();
  39.  
  40.             s3.Start();
  41.             //Reflection
  42.             for( var j = 0; j < 10000000; j++ )
  43.             {
  44.                 parser( str, out o );
  45.             }
  46.             s3.Stop();
  47.  
  48.             amount1 += s1.Elapsed;
  49.             amount2 += s2.Elapsed;
  50.             amount3 += s3.Elapsed;
  51.             cnt++;
  52.             Console.WriteLine( $"Actual: m={s1.Elapsed} d={s2.Elapsed} r={s3.Elapsed} Average: m={new TimeSpan( amount1.Ticks / cnt )} d={new TimeSpan( amount2.Ticks / cnt )} r={new TimeSpan( amount3.Ticks / cnt )} Count: {cnt}" );
  53.             s1.Reset();
  54.             s2.Reset();
  55.             s3.Reset();
  56.         }
  57.     }
  58. }
  59.  
  60. public static class ParserHelper
  61. {
  62.     public delegate bool TryParseDl( string str, out object obj );
  63.  
  64.     private static readonly HashSet<Type> ParsableStructs = new HashSet<Type>
  65.                                                                     {
  66.                                                                         typeof(int),
  67.                                                                         typeof(uint),
  68.                                                                         typeof(decimal),
  69.                                                                         typeof(short),
  70.                                                                         typeof(ushort),
  71.                                                                         typeof(double),
  72.                                                                         typeof(long),
  73.                                                                         typeof(ulong),
  74.                                                                         typeof(float),
  75.                                                                         typeof(byte),
  76.                                                                         typeof(sbyte)
  77.                                                                     };
  78.  
  79.     public static readonly ReadOnlyDictionary<Type, TryParseDl> StructParsers;
  80.  
  81.     static ParserHelper()
  82.     {
  83.         StructParsers = new ReadOnlyDictionary<Type, TryParseDl>( CreateParsersForStructs() );
  84.     }
  85.  
  86.     /// Creates parsers for structs
  87.     private static Dictionary<Type, TryParseDl> CreateParsersForStructs()
  88.     {
  89.         var parsers = new Dictionary<Type, TryParseDl>();
  90.         foreach( var t in ParsableStructs )
  91.         {
  92.             parsers[t] = GetParserForStruct( t );
  93.         }
  94.         return parsers;
  95.     }
  96.  
  97.     private static TryParseDl GetParserForStruct( Type targetType )
  98.     {
  99.         var methodInfo = targetType.GetMethod(
  100.                 "TryParse",
  101.                 BindingFlags.Public | BindingFlags.Static,
  102.                 Type.DefaultBinder,
  103.                 new[] { typeof(string), targetType.MakeByRefType() },
  104.                 null);
  105.  
  106.         return ( string str, out object obj ) =>
  107.         {
  108.             if( string.IsNullOrEmpty( str ) )
  109.             {
  110.                 obj = targetType.IsValueType ? Activator.CreateInstance( targetType ) : null;
  111.                 return true;
  112.             }
  113.             var inputParameters = new object[] { str, null };
  114.             var tryParseResult = (bool)methodInfo.Invoke(null, inputParameters);
  115.             obj = inputParameters[1];
  116.             return tryParseResult;
  117.         };
  118.     }
  119. }
  120.  
  121. public static class ExpressionBuilder
  122. {
  123.     public delegate bool TryParse( string str, out object obj );
  124.  
  125.     public static readonly ReadOnlyDictionary<Type, TryParse> StructParsers;
  126.  
  127.     static ExpressionBuilder()
  128.     {
  129.         StructParsers = new ReadOnlyDictionary<Type, TryParse>( CreateParsersForStructs() );
  130.     }
  131.  
  132.     /// Creates parsers for structs
  133.     /// </summary>
  134.     /// <returns>Dictionary</returns>
  135.     private static Dictionary<Type, TryParse> CreateParsersForStructs()
  136.     {
  137.  
  138.         var parsers = new Dictionary<Type, TryParse>();
  139.         parsers[typeof( int )] = StructParserExtensions.IntToObjParse;
  140.         parsers[typeof( uint )] = StructParserExtensions.UintToObjParse;
  141.         parsers[typeof( decimal )] = StructParserExtensions.DecimalToObjParse;
  142.         parsers[typeof( short )] = StructParserExtensions.ShortToObjParse;
  143.         parsers[typeof( ushort )] = StructParserExtensions.UshortToObjParse;
  144.         parsers[typeof( double )] = StructParserExtensions.DoubleToObjParse;
  145.         parsers[typeof( long )] = StructParserExtensions.LongToObjParse;
  146.         parsers[typeof( ulong )] = StructParserExtensions.UlongToObjParse;
  147.         parsers[typeof( float )] = StructParserExtensions.FloatToObjParse;
  148.         parsers[typeof( byte )] = StructParserExtensions.ByteToObjParse;
  149.         parsers[typeof( sbyte )] = StructParserExtensions.SbyteToObjParse;
  150.         return parsers;
  151.     }
  152. }
  153.  
  154. /// <summary>
  155. /// The struct parser extensions.
  156. /// </summary>
  157. public static class StructParserExtensions
  158. {
  159.     /// <summary>
  160.     /// Parses string representation of byte to obj.
  161.     /// </summary>
  162.     /// <param name="str">
  163.     /// The str.
  164.     /// </param>
  165.     /// <param name="obj">
  166.     /// The obj.
  167.     /// </param>
  168.     /// <returns>
  169.     /// The <see cref="bool"/>.
  170.     /// </returns>
  171.     public static bool ByteToObjParse( string str, out object obj )
  172.     {
  173.         byte i;
  174.         var result = byte.TryParse(str, out i);
  175.         obj = result ? (object)i : null;
  176.         return result;
  177.     }
  178.  
  179.     /// <summary>
  180.     /// Parses string representation of decimal to obj.
  181.     /// </summary>
  182.     /// <param name="str">
  183.     /// The str.
  184.     /// </param>
  185.     /// <param name="obj">
  186.     /// The obj.
  187.     /// </param>
  188.     /// <returns>
  189.     /// The <see cref="bool"/>.
  190.     /// </returns>
  191.     public static bool DecimalToObjParse( string str, out object obj )
  192.     {
  193.         decimal i;
  194.         var result = decimal.TryParse(str, out i);
  195.         obj = result ? (object)i : null;
  196.         return result;
  197.     }
  198.  
  199.     /// <summary>
  200.     /// Parses string representation of double to obj.
  201.     /// </summary>
  202.     /// <param name="str">
  203.     /// The str.
  204.     /// </param>
  205.     /// <param name="obj">
  206.     /// The obj.
  207.     /// </param>
  208.     /// <returns>
  209.     /// The <see cref="bool"/>.
  210.     /// </returns>
  211.     public static bool DoubleToObjParse( string str, out object obj )
  212.     {
  213.         double i;
  214.         var result = double.TryParse(str, out i);
  215.         obj = result ? (object)i : null;
  216.         return result;
  217.     }
  218.  
  219.     /// <summary>
  220.     /// Parses string representation of  float to obj.
  221.     /// </summary>
  222.     /// <param name="str">
  223.     /// The str.
  224.     /// </param>
  225.     /// <param name="obj">
  226.     /// The obj.
  227.     /// </param>
  228.     /// <returns>
  229.     /// The <see cref="bool"/>.
  230.     /// </returns>
  231.     public static bool FloatToObjParse( string str, out object obj )
  232.     {
  233.         float i;
  234.         var result = float.TryParse(str, out i);
  235.         obj = result ? (object)i : null;
  236.         return result;
  237.     }
  238.  
  239.     /// <summary>
  240.     /// Parses string representation of int to obj.
  241.     /// </summary>
  242.     /// <param name="str">
  243.     /// The str.
  244.     /// </param>
  245.     /// <param name="obj">
  246.     /// The obj.
  247.     /// </param>
  248.     /// <returns>
  249.     /// The <see cref="bool"/>.
  250.     /// </returns>
  251.     public static bool IntToObjParse( string str, out object obj )
  252.     {
  253.         int i;
  254.         var result = int.TryParse(str, out i);
  255.         obj = i;
  256.         return result;
  257.     }
  258.  
  259.     /// <summary>
  260.     /// Parses string representation of long to obj.
  261.     /// </summary>
  262.     /// <param name="str">
  263.     /// The str.
  264.     /// </param>
  265.     /// <param name="obj">
  266.     /// The obj.
  267.     /// </param>
  268.     /// <returns>
  269.     /// The <see cref="bool"/>.
  270.     /// </returns>
  271.     public static bool LongToObjParse( string str, out object obj )
  272.     {
  273.         long i;
  274.         var result = long.TryParse(str, out i);
  275.         obj = result ? (object)i : null;
  276.         return result;
  277.     }
  278.  
  279.     /// <summary>
  280.     /// Parses string representation of sbyte to obj.
  281.     /// </summary>
  282.     /// <param name="str">
  283.     /// The str.
  284.     /// </param>
  285.     /// <param name="obj">
  286.     /// The obj.
  287.     /// </param>
  288.     /// <returns>
  289.     /// The <see cref="bool"/>.
  290.     /// </returns>
  291.     public static bool SbyteToObjParse( string str, out object obj )
  292.     {
  293.         sbyte i;
  294.         var result = sbyte.TryParse(str, out i);
  295.         obj = result ? (object)i : null;
  296.         return result;
  297.     }
  298.  
  299.     /// <summary>
  300.     /// Parses string representation of short to obj.
  301.     /// </summary>
  302.     /// <param name="str">
  303.     /// The str.
  304.     /// </param>
  305.     /// <param name="obj">
  306.     /// The obj.
  307.     /// </param>
  308.     /// <returns>
  309.     /// The <see cref="bool"/>.
  310.     /// </returns>
  311.     public static bool ShortToObjParse( string str, out object obj )
  312.     {
  313.         short i;
  314.         var result = short.TryParse(str, out i);
  315.         obj = result ? (object)i : null;
  316.         return result;
  317.     }
  318.  
  319.     /// <summary>
  320.     /// Parses string representation of uint to obj.
  321.     /// </summary>
  322.     /// <param name="str">
  323.     /// The str.
  324.     /// </param>
  325.     /// <param name="obj">
  326.     /// The obj.
  327.     /// </param>
  328.     /// <returns>
  329.     /// The <see cref="bool"/>.
  330.     /// </returns>
  331.     public static bool UintToObjParse( string str, out object obj )
  332.     {
  333.         uint i;
  334.         var result = uint.TryParse(str, out i);
  335.         obj = result ? (object)i : null;
  336.         return result;
  337.     }
  338.  
  339.     /// <summary>
  340.     /// Parses string representation of ulong to obj.
  341.     /// </summary>
  342.     /// <param name="str">
  343.     /// The str.
  344.     /// </param>
  345.     /// <param name="obj">
  346.     /// The obj.
  347.     /// </param>
  348.     /// <returns>
  349.     /// The <see cref="bool"/>.
  350.     /// </returns>
  351.     public static bool UlongToObjParse( string str, out object obj )
  352.     {
  353.         ulong i;
  354.         var result = ulong.TryParse(str, out i);
  355.         obj = result ? (object)i : null;
  356.         return result;
  357.     }
  358.  
  359.     /// <summary>
  360.     /// Parses string representation of ushort to obj.
  361.     /// </summary>
  362.     /// <param name="str">
  363.     /// The str.
  364.     /// </param>
  365.     /// <param name="obj">
  366.     /// The obj.
  367.     /// </param>
  368.     /// <returns>
  369.     /// The <see cref="bool"/>.
  370.     /// </returns>
  371.     public static bool UshortToObjParse( string str, out object obj )
  372.     {
  373.         ushort i;
  374.         var result = ushort.TryParse(str, out i);
  375.         obj = result ? (object)i : null;
  376.         return result;
  377.     }
  378. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement