System.Web.UI.DataBinder.Eval
mechanism and the question arose: how fast does it work and is it possible to perform such an operation even faster?public class c1
{
string _str;
public c1( string str)
{
_str = str;
}
public string Str1 { get { return "1: " +_str; } }
public virtual string Str2 { get { return "2: " +_str; } }
}
public class c2 : c1
{
public c2( string str)
: base (str)
{
}
public override string Str2 { get { return "C" + base .Str2; } }
}
c2 c = new c2( "str" );
//
for ( int i=0; i<ITERATIONS; i++)
{
String .Format( "Str1: {0}, Str2: {1}" , c.Str1, c.Str2);
}
// DataBinder
for ( int i=0; i<ITERATIONS; i++)
{
String .Format( "Str1: {0}, Str2: {1}" , DataBinder.Eval(c, "Str1" ), DataBinder.Eval(c, "Str2" ));
}
c2 c = new c2( "str" );
PropertyAccessor p1 = PEval.GetPropertyAccessor(c.GetType(), "Str1" );
PropertyAccessor p2 = PEval.GetPropertyAccessor(c.GetType(), "Str2" );
for ( int i=0; i<ITERATIONS; i++)
{
String .Format( "Str1: {0}, Str2: {1}" , p1.GetValue( c ), p2.GetValue( c ));
}
c2 c = new c2( "str" );
for ( int i=0; i<ITERATIONS; i++)
{
String .Format( "Str1: {0}, Str2: {1}" , PEval.GetValue(c, "Str1" ), PEval.GetValue(c, "Str2" ));
}
Create PropertyAccessor for TestDataBinding.c1.Str1
Create PropertyAccessor for TestDataBinding.c2.Str2
=========================
ITERATIONS: 100000
: 00:00:00.065 (100%)
PEval.GetValue: 00:00:00.090 (138%)
: 00:00:00.065 (100%)
DataBinder.Eval: 00:00:01.135 (1746%)
=========================
TestDataBinding.c2 (TestDataBinding.PropEvaluator.PropertyAccessor) [200003]
TestDataBinding.c1_Str1_Accessor => Str1 (G) [100002]
TestDataBinding.c2_Str2_Accessor => Str2 (G) [100001]
TestDataBinding.c1 (TestDataBinding.PropEvaluator.PropertyAccessor) [2]
TestDataBinding.c1_Str1_Accessor => Str1 (G) [100002]
=========================
=========================
ITERATIONS: 1000000
: 00:00:00.930 (100%)
PEval.GetValue: 00:00:01.085 (116%)
: 00:00:00.738 (79%)
DataBinder.Eval: 00:00:10.976 (1180%)
=========================
=========================
ITERATIONS: 10000000
: 00:00:06.802 (100%)
PEval.GetValue: 00:00:10.917 (160%)
: 00:00:07.017 (103%)
DataBinder.Eval: 00:01:45.476 (1550%)
=========================
PEval.GetValue(o, "Prop1.Prop2.Prop4")
chain PEval.GetValue(o, "Prop1.Prop2.Prop4")
Source: https://habr.com/ru/post/73352/
All Articles