pythonweb开发几个模板系统的性能对比

上传人:自*** 文档编号:79098285 上传时间:2019-02-16 格式:DOCX 页数:3 大小:45.67KB
返回 下载 相关 举报
pythonweb开发几个模板系统的性能对比_第1页
第1页 / 共3页
pythonweb开发几个模板系统的性能对比_第2页
第2页 / 共3页
pythonweb开发几个模板系统的性能对比_第3页
第3页 / 共3页
亲,该文档总共3页,全部预览完了,如果喜欢就下载吧!
资源描述

《pythonweb开发几个模板系统的性能对比》由会员分享,可在线阅读,更多相关《pythonweb开发几个模板系统的性能对比(3页珍藏版)》请在金锄头文库上搜索。

1、Python web开发:几个模板系统的性能对比对比目标,jinja2,cheetah,mako,webpy,bottle,tornado,django的性能。方法,随机生成一个二维数组,第一列是自增数据,第二列是长度为100的随机字符串,然后生成html,比较一次生成的时间。说明,如果模板有编译缓存,打开。有其他方法加速,打开。生成缓存,关闭。不计算随机数据生成时间,一次生成后一直使用。以下是文件有效内容,没用的都略去了。最后的顺序是因为我根据结果整理了一下调用次序。testcheetah.tmpl#for $i in $l#end for$i0$i1testdjango.html% for

2、 i in l % endfor % i.0 i.1 testjinja2.html% for i in l % endfor % i0 i1 testmako.html% for i in l:% endfor$i0$i1testwebpy.html$def with(l)$for i in l:$i0$i1tmpl.py#!/usr/bin/python# - coding: utf-8 -”date: 2011-11-03author: shell.xu”import os, random, string, timeittestdata = def init_testdata():for

3、 i in xrange(1000):s = ”.join(random.choice(string.letters) for j in xrange(100)testdata.append(i, s)init_testdata()# webpyimport webrender = web.template.render(./)def render_webpy():return render.testwebpy(testdata)# jinja2from jinja2 import Environment, FileSystemLoader, FileSystemBytecodeCacheen

4、v = Environment(loader = FileSystemLoader(./),bytecode_cache = FileSystemBytecodeCache(./, %s.cache)tmpl_jinja = env.get_template(testjinja2.html)def render_jinja2():return tmpl_jinja.render(l = testdata)# cheetahfrom testcheetah import testcheetahdef render_cheetah():return testcheetah(searchList =

5、 l: testdata,)# makofrom mako.template import Template as makotmpltmpl_mako = makotmpl(filename = ./testmako.html)def render_mako():return tmpl_mako.render(l = testdata)# djangofrom django.template import Template as djangotmplfrom django.template import Contextfrom django.conf import settingssettin

6、gs.configure()with open(testdjango.html, r) as fi: tmpl_django = djangotmpl(fi.read()def render_django():return tmpl_django.render(Context(l: testdata)# bottlefrom bottle import SimpleTemplatewith open(testbottle.html, r) as fi: tmpl_bottle = SimpleTemplate(fi.read()def render_bottle():return tmpl_b

7、ottle.render(l = testdata)# tornadofrom tornado import template as tornado_tmplwith open(testtornado.html, r) as fi: tmpl_tornado = tornado_tmpl.Template(fi.read()def render_tornado():return tmpl_tornado.generate(l = testdata)def testfunc(funcname, times = 10000):from timeit import Timert = Timer(“%

8、s()” % funcname, “from _main_ import ”)print funcname: %s used %f % (funcname, t.timeit(times) / times)if _name_ = _main_:testfunc(render_django, times = 1000)testfunc(render_webpy, times = 1000)testfunc(render_bottle, times = 10000)testfunc(render_tornado, times = 10000)testfunc(render_jinja2, time

9、s = 10000)testfunc(render_mako, times = 10000)testfunc(render_cheetah, times = 100000)以下是运行结果。funcname: render_django used 0.071762funcname: render_webpy used 0.015729funcname: render_bottle used 0.008752funcname: render_tornado used 0.005675funcname: render_jinja2 used 0.002073funcname: render_mako

10、 used 0.001627funcname: render_cheetah used 0.000014点评一下吧。django就是个渣,不多废话了。webpy的代码很简洁,可惜速度太慢了。bottle看起来快一点,不过也没有多出彩。 tornado本身速度很快,不过模板也就是如此吧。真的值得一用的,只有jinja2,mako,cheetah三个。速度都小于了5ms,单核每 秒可以生成200个页面,16核机器上大概就能跑到3000req/s,性能比较高。jinja2的速度比较折衷,配置灵活,语法类似django是他的 优点。而且不得不说,jinja2的文档真的很不错。mako的速度比jinja

11、2略快,模板写起来也很舒服。文档略凌乱,可以接受。cheetah的速 度已经不像是模板了好吧。这个东西是使用编译器将模板编译为py文件,然后再通过python编译为pyc,从而获得如此高的性能的。如果python可以执行加速(例如 psyco, pypy什么的),相信速度还要快。但是不得不说,语法实在是太严格了一点。我在for前面多了一个空格,居然直接报错,而且还是一个无关错误。找起问题 来相当困难。不过,对于习惯了python格式的格式控来说,cheetah还是有相当价值的。cheetah加速后的速度,单核上每秒可以生成7W多个 页面,16核的普通服务器,每秒可以承载100W req/s。看在效率的份上,我可以原谅他大多数的问题。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号