Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Collections.ObjectModel;
- using System.Diagnostics;
- public class Program
- {
- public static void Main()
- {
- TimeSpan amount1 = TimeSpan.Zero, amount2 = TimeSpan.Zero, amount3 = TimeSpan.Zero;
- var cnt = 0;
- var s1 = new Stopwatch();
- var s2 = new Stopwatch();
- var s3 = new Stopwatch();
- const string str = "100";
- int i;
- var parser2 = ExpressionBuilder.StructParsers[typeof(int)];
- object o2;
- var parser = ParserHelper.StructParsers[typeof(int)];
- object o;
- while( true )
- {
- //Method
- s1.Start();
- for( var j = 0; j < 10000000; j++ )
- {
- int.TryParse( str, out i );
- }
- s1.Stop();
- //Delegate
- s2.Start();
- for( var j = 0; j < 10000000; j++ )
- parser2( str, out o2 );
- s2.Stop();
- s3.Start();
- //Reflection
- for( var j = 0; j < 10000000; j++ )
- {
- parser( str, out o );
- }
- s3.Stop();
- amount1 += s1.Elapsed;
- amount2 += s2.Elapsed;
- amount3 += s3.Elapsed;
- cnt++;
- 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}" );
- s1.Reset();
- s2.Reset();
- s3.Reset();
- }
- }
- }
- public static class ParserHelper
- {
- public delegate bool TryParseDl( string str, out object obj );
- private static readonly HashSet<Type> ParsableStructs = new HashSet<Type>
- {
- typeof(int),
- typeof(uint),
- typeof(decimal),
- typeof(short),
- typeof(ushort),
- typeof(double),
- typeof(long),
- typeof(ulong),
- typeof(float),
- typeof(byte),
- typeof(sbyte)
- };
- public static readonly ReadOnlyDictionary<Type, TryParseDl> StructParsers;
- static ParserHelper()
- {
- StructParsers = new ReadOnlyDictionary<Type, TryParseDl>( CreateParsersForStructs() );
- }
- /// Creates parsers for structs
- private static Dictionary<Type, TryParseDl> CreateParsersForStructs()
- {
- var parsers = new Dictionary<Type, TryParseDl>();
- foreach( var t in ParsableStructs )
- {
- parsers[t] = GetParserForStruct( t );
- }
- return parsers;
- }
- private static TryParseDl GetParserForStruct( Type targetType )
- {
- var methodInfo = targetType.GetMethod(
- "TryParse",
- BindingFlags.Public | BindingFlags.Static,
- Type.DefaultBinder,
- new[] { typeof(string), targetType.MakeByRefType() },
- null);
- return ( string str, out object obj ) =>
- {
- if( string.IsNullOrEmpty( str ) )
- {
- obj = targetType.IsValueType ? Activator.CreateInstance( targetType ) : null;
- return true;
- }
- var inputParameters = new object[] { str, null };
- var tryParseResult = (bool)methodInfo.Invoke(null, inputParameters);
- obj = inputParameters[1];
- return tryParseResult;
- };
- }
- }
- public static class ExpressionBuilder
- {
- public delegate bool TryParse( string str, out object obj );
- public static readonly ReadOnlyDictionary<Type, TryParse> StructParsers;
- static ExpressionBuilder()
- {
- StructParsers = new ReadOnlyDictionary<Type, TryParse>( CreateParsersForStructs() );
- }
- /// Creates parsers for structs
- /// </summary>
- /// <returns>Dictionary</returns>
- private static Dictionary<Type, TryParse> CreateParsersForStructs()
- {
- var parsers = new Dictionary<Type, TryParse>();
- parsers[typeof( int )] = StructParserExtensions.IntToObjParse;
- parsers[typeof( uint )] = StructParserExtensions.UintToObjParse;
- parsers[typeof( decimal )] = StructParserExtensions.DecimalToObjParse;
- parsers[typeof( short )] = StructParserExtensions.ShortToObjParse;
- parsers[typeof( ushort )] = StructParserExtensions.UshortToObjParse;
- parsers[typeof( double )] = StructParserExtensions.DoubleToObjParse;
- parsers[typeof( long )] = StructParserExtensions.LongToObjParse;
- parsers[typeof( ulong )] = StructParserExtensions.UlongToObjParse;
- parsers[typeof( float )] = StructParserExtensions.FloatToObjParse;
- parsers[typeof( byte )] = StructParserExtensions.ByteToObjParse;
- parsers[typeof( sbyte )] = StructParserExtensions.SbyteToObjParse;
- return parsers;
- }
- }
- /// <summary>
- /// The struct parser extensions.
- /// </summary>
- public static class StructParserExtensions
- {
- /// <summary>
- /// Parses string representation of byte to obj.
- /// </summary>
- /// <param name="str">
- /// The str.
- /// </param>
- /// <param name="obj">
- /// The obj.
- /// </param>
- /// <returns>
- /// The <see cref="bool"/>.
- /// </returns>
- public static bool ByteToObjParse( string str, out object obj )
- {
- byte i;
- var result = byte.TryParse(str, out i);
- obj = result ? (object)i : null;
- return result;
- }
- /// <summary>
- /// Parses string representation of decimal to obj.
- /// </summary>
- /// <param name="str">
- /// The str.
- /// </param>
- /// <param name="obj">
- /// The obj.
- /// </param>
- /// <returns>
- /// The <see cref="bool"/>.
- /// </returns>
- public static bool DecimalToObjParse( string str, out object obj )
- {
- decimal i;
- var result = decimal.TryParse(str, out i);
- obj = result ? (object)i : null;
- return result;
- }
- /// <summary>
- /// Parses string representation of double to obj.
- /// </summary>
- /// <param name="str">
- /// The str.
- /// </param>
- /// <param name="obj">
- /// The obj.
- /// </param>
- /// <returns>
- /// The <see cref="bool"/>.
- /// </returns>
- public static bool DoubleToObjParse( string str, out object obj )
- {
- double i;
- var result = double.TryParse(str, out i);
- obj = result ? (object)i : null;
- return result;
- }
- /// <summary>
- /// Parses string representation of float to obj.
- /// </summary>
- /// <param name="str">
- /// The str.
- /// </param>
- /// <param name="obj">
- /// The obj.
- /// </param>
- /// <returns>
- /// The <see cref="bool"/>.
- /// </returns>
- public static bool FloatToObjParse( string str, out object obj )
- {
- float i;
- var result = float.TryParse(str, out i);
- obj = result ? (object)i : null;
- return result;
- }
- /// <summary>
- /// Parses string representation of int to obj.
- /// </summary>
- /// <param name="str">
- /// The str.
- /// </param>
- /// <param name="obj">
- /// The obj.
- /// </param>
- /// <returns>
- /// The <see cref="bool"/>.
- /// </returns>
- public static bool IntToObjParse( string str, out object obj )
- {
- int i;
- var result = int.TryParse(str, out i);
- obj = i;
- return result;
- }
- /// <summary>
- /// Parses string representation of long to obj.
- /// </summary>
- /// <param name="str">
- /// The str.
- /// </param>
- /// <param name="obj">
- /// The obj.
- /// </param>
- /// <returns>
- /// The <see cref="bool"/>.
- /// </returns>
- public static bool LongToObjParse( string str, out object obj )
- {
- long i;
- var result = long.TryParse(str, out i);
- obj = result ? (object)i : null;
- return result;
- }
- /// <summary>
- /// Parses string representation of sbyte to obj.
- /// </summary>
- /// <param name="str">
- /// The str.
- /// </param>
- /// <param name="obj">
- /// The obj.
- /// </param>
- /// <returns>
- /// The <see cref="bool"/>.
- /// </returns>
- public static bool SbyteToObjParse( string str, out object obj )
- {
- sbyte i;
- var result = sbyte.TryParse(str, out i);
- obj = result ? (object)i : null;
- return result;
- }
- /// <summary>
- /// Parses string representation of short to obj.
- /// </summary>
- /// <param name="str">
- /// The str.
- /// </param>
- /// <param name="obj">
- /// The obj.
- /// </param>
- /// <returns>
- /// The <see cref="bool"/>.
- /// </returns>
- public static bool ShortToObjParse( string str, out object obj )
- {
- short i;
- var result = short.TryParse(str, out i);
- obj = result ? (object)i : null;
- return result;
- }
- /// <summary>
- /// Parses string representation of uint to obj.
- /// </summary>
- /// <param name="str">
- /// The str.
- /// </param>
- /// <param name="obj">
- /// The obj.
- /// </param>
- /// <returns>
- /// The <see cref="bool"/>.
- /// </returns>
- public static bool UintToObjParse( string str, out object obj )
- {
- uint i;
- var result = uint.TryParse(str, out i);
- obj = result ? (object)i : null;
- return result;
- }
- /// <summary>
- /// Parses string representation of ulong to obj.
- /// </summary>
- /// <param name="str">
- /// The str.
- /// </param>
- /// <param name="obj">
- /// The obj.
- /// </param>
- /// <returns>
- /// The <see cref="bool"/>.
- /// </returns>
- public static bool UlongToObjParse( string str, out object obj )
- {
- ulong i;
- var result = ulong.TryParse(str, out i);
- obj = result ? (object)i : null;
- return result;
- }
- /// <summary>
- /// Parses string representation of ushort to obj.
- /// </summary>
- /// <param name="str">
- /// The str.
- /// </param>
- /// <param name="obj">
- /// The obj.
- /// </param>
- /// <returns>
- /// The <see cref="bool"/>.
- /// </returns>
- public static bool UshortToObjParse( string str, out object obj )
- {
- ushort i;
- var result = ushort.TryParse(str, out i);
- obj = result ? (object)i : null;
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement