关于tornado 自带的 template 模版引擎和tenjin template 性能比较。
tornado 代码 https://github.com/tornadoweb/tornado/blob/master/demos/benchmark/template_benchmark.py
使用tenjin template 的代码如下:
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
#!/usr/bin/env python
#
# A simple benchmark of tornado template rendering, based on
# https://github.com/mitsuhiko/jinja2/blob/master/examples/bench.py
import sys
from timeit import Timer
from tornado.options import options, define, parse_command_line
from tenjin import Template
# from tenjin.html import *
from tenjin.helpers import *
define('num', default=100, help='number of iterations')
define('dump', default=False, help='print template generated code and exit')
context = {
'page_title': 'mitsuhiko\'s benchmark',
'table': [dict(a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9,j=10) for x in range(1000)]
}
tmpl = Template(input="""\
<!doctype html>
<html>
<head>
<title>#{ page_title }</title>
</head>
<body>
<div class="header">
<h1>#{ page_title }</h1>
</div>
<ul class="navigation">
<?py for href, caption in [ \
('index.html', 'Index'), \
('downloads.html', 'Downloads'), \
('products.html', 'Products') \
]: ?>
<li><a href="#{ href }">#{ caption }</a></li>
<?py #endfor ?>
</ul>
<div class="table">
<table>
<?py for row in table: ?>
<tr>
<?py for cell in row: ?>
<td>#{ cell }</td>
<?py #endfor ?>
</tr>
<?py #endfor ?>
</table>
</div>
</body>
</html>\
""")
def render():
tmpl.render(context)
def main():
parse_command_line()
if options.dump:
print(tmpl.code)
sys.exit(0)
t = Timer(render)
results = t.timeit(options.num) / options.num
print('%0.3f ms per iteration' % (results*1000))
if __name__ == '__main__':
main()
结果:
1
2
3
4
$ python tenjin.py
5.947 ms per iteration
$ python tnd.py
24.822 ms per iteration
tenjin 速度是tornado 的 6 倍。
golang text/template 平均 12 ms
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
package main
import (
"bytes"
"fmt"
"text/template"
"time"
)
func main() {
// Define a template.
const letter = `
{{define "T"}}
<!doctype html>
<html>
<head>
<title>{{ .Page_title }}</title>
</head>
<body>
<div class="header">
<h1>{{ .Page_title }}</h1>
</div>
<ul class="navigation">
{{ range $pipeliner, $href := .Caption }}
<li><a href="{{ $pipeliner }}">{{ $href }}</a></li>
{{ end }}
</ul>
<div class="table">
<table>
{{ range $_, $row := .Table }}
<tr>
{{ range $_, $cell := $row }}
<td>{{ $cell }}</td>
{{ end }}
</tr>
{{ end }}
</table>
</div>
</body>
</html>
{{end}}
`
//
caption := map[string]string{}
caption["index.html"] = "Index"
caption["downloads.html"] = "Downloads"
caption["products.html"] = "Products"
//
dict := map[string]int{}
dict["a"] = 1
dict["b"] = 2
dict["c"] = 3
dict["d"] = 4
dict["e"] = 5
dict["f"] = 6
dict["g"] = 7
dict["h"] = 8
dict["i"] = 9
dict["j"] = 10
fmt.Println(dict)
table := []map[string]int{}
for i := 0; i < 1000; i++ {
table = append(table, dict)
}
fmt.Println(len(table))
t1 := time.Now()
t := template.Must(template.New("T").Parse(letter))
data := struct {
Page_title string
Caption map[string]string
Table []map[string]int
}{
"mitsuhiko s benchmark",
caption,
table,
}
var doc bytes.Buffer
for i := 0; i < 1000; i++ {
t.ExecuteTemplate(&doc, "T", data)
}
t2 := time.Now()
fmt.Println("消耗时间:", t2.Sub(t1), "秒")
fmt.Println(t2.Sub(t1) / 1000)
}
1
2
消耗时间: 12.084226069s 秒
12.084226ms
总结
从结果看,golang 标准库模版渲染速度与tonardo 自带的模版速度相当,tenjin 速度是它们的 6 倍。
👍
本文网址: https://pylist.com/topic/118.html 转摘请注明来源