Is there any reason or anything I am doing wrong, why RazorEngine is so slow just to parse 100 different templates? I was looking into StringTemplate, and performed two tests to compare, as per below.
[Test]
public void TestStringTemplate()
{
CsStopwatch stopwatch = new CsStopwatch();
stopwatch.Start();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 100; i++)
{
string template = @"Hello there my name is <Name> <Surname> " + i;
TextParseTests.TestModel model = new TextParseTests.TestModel();
model.Name = "Karl";
model.Surname = "Cassar";
Template t = new Template(template);
t.Add("Name", model.Name);
t.Add("Surname", model.Surname);
var result = t.Render();
sb.AppendLine(result);
}
stopwatch.Stop();
var ms = stopwatch.ElapsedMilliseconds;
int k = 5;
//109ms
}
[Test]
public void TestRazorEngine()
{
CsStopwatch stopwatch = new CsStopwatch();
stopwatch.Start();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 100; i++)
{
string template = @"Hello there my name is @Model.Name @Model.Surname " + i;
TextParseTests.TestModel model = new TextParseTests.TestModel();
model.Name = "Karl";
model.Surname = "Cassar";
var result = Razor.Parse(template, model);
sb.AppendLine(result);
}
stopwatch.Stop();
var ms = stopwatch.ElapsedMilliseconds;
int k = 5;
//24000~ ms
}
The difference is staggering.
That is over 200x slower than StringTemplate! I have a lot of content using the RazorEngine format, and I prefer the syntax for RazorEngine way over StringTemplate. However, this is extremely, extremely slow.
Any ideas if I may be doing anything wrong? Please note that I am using different templates on purpose, as if I use caching for RazorEngine it is way faster (down to 300 - 400ms) but my website has a lot of tiny text which are different, and this is the most 'real-life' test I could do.
Razor.Parse will compile the template every time.
You shouldn't do this. Your application should compile the template once, or whenever the template is changed if the template is being modified whilst the application is running.
Razor.Compile(template, name);
Following this your application should use
Razor.Run(name, model);
Compare the times against Razor.Run not Razor.Parse