pyList

tornado template & tenjin template & golang template 性能比较

关于tornado 自带的 template 模版引擎和tenjin template 性能比较。

tornado 代码 https://github.com/tornadoweb/tornado/blob/master/demos/benchmark/template_benchmark.py 5

使用tenjin template 的代码如下:

#!/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()



结果:

$ python tenjin.py 
5.947 ms per iteration
$ python tnd.py 
24.822 ms per iteration

tenjin 速度是tornado 的 6 倍。

golang text/template 平均 12 ms

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)

}

消耗时间: 12.084226069s 秒
12.084226ms

总结

从结果看,golang 标准库模版渲染速度与tonardo 自带的模版速度相当,tenjin 速度是它们的 6 倍。

👍

本文标签: 模版 性能 Golang
本文网址: https://pylist.com/t/1441682856 (转载注明出处)
如果你有任何建议或疑问可以在下面 留言
发表第一条评论!
验证码图片
相关推荐