Let's start with a conclusion. 1) The razorengine is really slow, the compilation process is slow, and the compilation is still not suitable for a large number of repeated calls (one call can be accepted). 2) The relationship between Razorengine and ASP. Razor is unclear, so the conclusion here does not mean that ASP. NET MVC is slow. 3). NET 4.6 has a slightly higher reflection performance than. NET 2.0, and presumably the performance of the reflection assignment is raised from the native assignment of 1/5 to 1/4.
When testing razorengine here, an object property evaluator is implemented:
using system diagnostics
using razorengine
using razorengine templating
//脚本化的C#想写对还是有点麻烦的,还好Razor能给出有用的错误信息。
public class RazorPropertySetter<TEntity> where TEntity :class
{
string PropertyName;
bool Inited = false;
private RazorPropertySetter(){ }
public static RazorPropertySetter<TEntity> Create( string propertyName)
{
RazorPropertySetter<TEntity> rs = new RazorPropertySetter<TEntity>();
rs.PropertyName = propertyName;
return rs;
}
public void Set(TEntity entity , object value)
{
/span>string CacheKey = entity gettype (). fullname + "--" + this propertyname
DynamicViewBag viewBag = new DynamicViewBag();
viewBag.AddValue("PropertyValue", value);
if (Inited == false)
{
string template = "@{model." + propertyname + "= Viewbag.propertyvalue;}"
engine razor runcompile ( template CacheKey typeof tentity entity viewbag
Inited = true;
}
else
{
Engine.Razor.Run(cacheKey, typeof(TEntity), entity, viewBag);
}
}
}
//测试用类
public class TestData
{
public string Title { get; set; }
}
//测试代码
TestData data = new TestData();
data.Title = "V1";
/span>razorpropertysetter < testdata > Settitle = razorpropertysetter < testdata >. create
Stopwatch watch = new Stopwatch();
watch.Start();
setTitle.Set(data, "V00");
watch.Stop();
Performance Test Results:
When there is time to study the performance of Script.net.
From for notes (Wiz)
Razorengine Performance Studies (reflection of the extended depth)