tornado template & tenjin template & golang template 性能比较
🕛 by pyList at 2015-09-08 11:27
关于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 倍。
👍
本文网址: https://pylist.com/t/1441682856 (转载注明出处)
如果你有任何建议或疑问可以在下面 留言
发表第一条评论!
相关推荐
小工具
标签
tornado
py
sae
tenjin
模板
golang
网页
正文
提取
新闻
缓存
禁用
静态
文件
异步
网址
检测
目标
comet
推送
服务器
技术
webdriver
微博
selenium
登录
断言
类型
注意
问题
跨域
ajax
处理
构建
应用
一个
web
nginx
quic
抢先
bbr
openwrt
路由
开启
cjson
lua
module
错误
解决
国内
md5
计算
os
popen
python
超时
老旧
笔记本
改造
记录
低功耗
爬虫
组装
上传
中断
默认值
struct
设置
之坑
files
open
服务
google
身份验证
authenticator
迁移
手机
硬件加速
ubnt
er
固件
js
confirm
onclick
删除
确认
变砖
arris
ac1750
sbr
自动更新
microsoft
mac
关闭
app
store
未知
静音
风扇
主机
微信
尝鲜
视频
体验
cpu
debian
ubuntu
查看
温度
chrome
server
浏览
gnu
linux
安装
系统启动
usb
编译
宅家
坑记
屏幕
动手
useragent
搜索引擎
蜘蛛
真假
识别
最近发表
- Mac 关闭 Microsoft 自动更新
- Mac 登录 App Store 出现“发生了未知错误”的解决方法
- 老笔记本改造为无风扇静音主机方案
- 自己组装21瓦低功耗家庭爬虫、文件、web服务器
- 微信视频号尝鲜体验
- Ubuntu/Debian 查看CPU温度的方法
- 在Ubuntu/debian Server 系统使用Chrome 无头浏览模式
- 换手机后 Google 身份验证器 Google Authenticator 数据迁移的简单方法
- 使用Golang selenium WebDriver 自动登录微博
- 在 Ubuntu 或其它 GNU/Linux 系统下安装 Debian
- Mac 下制作 USB ubuntu/debian 系统启动、安装盘的几种方法
- ubuntu/debian 下自行编译 OpenWRT 固件
- 宅家自己动手换手机屏幕掉坑记
- 路由 UBNT ER-X 官方固件升级及开启硬件加速的方法
- 在 Nginx 和 Golang web 上抢先体验 QUIC
- 从UserAgent识别搜索引擎并判断真假蜘蛛
最近浏览
- Openwrt 路由上开启BBR
- 出现 lua module 'cjson' not found 错误的解决方法
- OpenWrt 国内源
- golang 计算大文件md5
- python 解决os.popen 超时问题
- 把老旧笔记本改造为家庭服务器过程记录
- 自己组装21瓦低功耗家庭爬虫、文件、web服务器
- Nginx 服务器上传大文件经常中断的解决方法
- go struct 设置默认值
- Golang 服务之坑:too many open files
- 换手机后 Google 身份验证器 Google Authenticator 数据迁移的简单方法
- 使用Golang selenium WebDriver 自动登录微博
- 路由 UBNT ER-X 官方固件升级及开启硬件加速的方法
- 删除确认 js confirm onclick
- ARRIS SBR-AC1750 路由变砖拆解折腾
- Mac 关闭 Microsoft 自动更新