-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathProgram.cs
109 lines (91 loc) · 3.11 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
public class Program
{
public static void Main()
{
Sample();
BenchTest();
}
private static void Sample()
{
// Mustache template
const string template_text =
@"{{! This is a spec-compliant mustache template }}
Hello {{name}} from Zig
This template was generated with
{{#env}}
Zig: {{zig_version}}
Mustache: {{mustache_version}}
{{/env}}
Supported features:
{{#features}}
- {{name}} {{condition}}
{{/features}}";
using var template = mustache.Mustache.CreateTemplate(template_text);
// Context, can be any C# class, object, enum, array, collection, etc
var ctx = new Context
{
name = "friends",
env = new Env
{
zig_version = "master",
mustache_version = "alpha",
},
features = new Feature[]
{
new Feature { name = "interpolation", condition = "✅ done" },
new Feature { name = "sections", condition = "✅ done" },
new Feature { name = "comments", condition = "✅ done" },
new Feature { name = "delimiters", condition = "✅ done" },
new Feature { name = "partials", condition = "✅ done" },
new Feature { name = "lambdas", condition = "✅ done" },
new Feature { name = "inheritance", condition = "⏳ comming soon" },
},
};
Console.WriteLine(mustache.Mustache.Render(template, ctx));
}
private static void BenchTest()
{
using var templete = mustache.Mustache.CreateTemplate("<title>{{title}}</title><h1>{{ title }}</h1><div>{{{body}}}</div>");
var data = new Data
{
title = "Hello, Mustache!",
body = "This is a really simple test of the rendering!",
};
Console.WriteLine("Rendering this simple template 1 million times\n{0}\n", mustache.Mustache.Render(templete, data));
long total = 0;
var watcher = System.Diagnostics.Stopwatch.StartNew();
for (int i = 0; i < 1_000_000; i++)
{
var value = mustache.Mustache.Render(templete, data);
total += value.Length;
}
watcher.Stop();
Console.WriteLine($"C# FFI");
Console.WriteLine($"Total time {watcher.Elapsed.TotalSeconds:0.000}s");
Console.WriteLine($"{1_000_000d / watcher.Elapsed.TotalSeconds:0} ops/s");
Console.WriteLine($"{watcher.ElapsedMilliseconds:0} ns/iter");
Console.WriteLine($"{((double)total / 1024d / 1024d) / watcher.Elapsed.TotalSeconds:0} MB/s");
}
public class Feature
{
public string? name { get; set; }
public string? condition { get; set; }
}
public class Env
{
public string? zig_version { get; set; }
public string? mustache_version { get; set; }
}
public class Context
{
public string? name { get; set; }
public Env? env { get; set; }
public Feature[]? features { get; set; }
}
public class Data
{
public string? title;
public string? body;
}
}