Python web开发:几个模板系统的性能对比

对比目标,jinja2,cheetah,mako,webpy,bottle,tornado,django的性能。 html

方法,随机生成一个二维数组,第一列是自增数据,第二列是长度为100的随机字符串,而后生成html,比较一次生成的时间。 python

说明,若是模板有编译缓存,打开。有其余方法加速,打开。生成缓存,关闭。不计算随机数据生成时间,一次生成后一直使用。 web

如下是文件有效内容,没用的都略去了。最后的顺序是由于我根据结果整理了一下调用次序。 shell

—–testcheetah.tmpl—–

#for $i in $l

#end for

$i[0]

$i[1]

—–testdjango.html—–

{% for i in l %}

{% endfor %}

{{ i.0 }}

{{ i.1 }}

—–testjinja2.html—–

{% for i in l %}

{% endfor %}

{{ i[0] }}

{{ i[1] }}

—–testmako.html—–

% for i in l:

% endfor

${i[0]}

${i[1]}

—–testwebpy.html—–

$def with(l)

$for i in l:

$i[0]

$i[1]

—–tmpl.py—–

#!/usr/bin/python

# -﹡- coding: utf-8 -﹡-

”’

@date: 2011-11-03

@author: shell.xu

”’

import os, random, string, timeit

testdata = []

def init_testdata():

for i in xrange(1000):

s = ”.join([random.choice(string.letters) for j in xrange(100)])

testdata.append((i, s))

init_testdata()

# ——–webpy——–

import web

render = web.template.render(‘./’)

def render_webpy():

return render.testwebpy(testdata)

# ——–jinja2——–

from jinja2 import Environment, FileSystemLoader, FileSystemBytecodeCache

env = Environment(loader = FileSystemLoader(‘./’),

bytecode_cache = FileSystemBytecodeCache(‘./’, ‘%s.cache’))

tmpl_jinja = env.get_template(‘testjinja2.html’)

def render_jinja2():

return tmpl_jinja.render(l = testdata)

# ——–cheetah——–

from testcheetah import testcheetah

def render_cheetah():

return testcheetah(searchList = [{'l': testdata},])

# ——–mako——–

from mako.template import Template as makotmpl

tmpl_mako = makotmpl(filename = ‘./testmako.html’)

def render_mako():

return tmpl_mako.render(l = testdata)

# ——–django——–

from django.template import Template as djangotmpl

from django.template import Context

from django.conf import settings

settings.configure()

with open(‘testdjango.html’, ‘r’) as fi: tmpl_django = djangotmpl(fi.read())

def render_django():

return tmpl_django.render(Context({‘l’: testdata}))

# ——–bottle——–

from bottle import SimpleTemplate

with open(‘testbottle.html’, ‘r’) as fi: tmpl_bottle = SimpleTemplate(fi.read())

def render_bottle():

return tmpl_bottle.render(l = testdata)

# ——–tornado——–

from tornado import template as tornado_tmpl

with 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 Timer

t = Timer(“%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′, times = 10000)

testfunc(‘render_mako’, times = 10000)

testfunc(‘render_cheetah’, times = 100000)
如下是运行结果:

funcname: render_django used 0.071762 django

funcname: render_webpy used 0.015729 数组

funcname: render_bottle used 0.008752 缓存

funcname: render_tornado used 0.005675 服务器

funcname: render_jinja2 used 0.002073 app

funcname: render_mako used 0.001627 dom

funcname: render_cheetah used 0.000014

点评一下吧。django就是个渣,很少废话了。webpy的代码很简洁,惋惜速度太慢了。bottle看起来快一点,不过也没有多出彩。 tornado自己速度很快,不过模板——也就是如此吧。真的值得一用的,只有jinja2,mako,cheetah三个。速度都小于了5ms,单核每 秒能够生成200个页面,16核机器上大概就能跑到3000req/s,性能比较高。jinja2的速度比较折衷,配置灵活,语法相似django是他的 优势。并且不得不说,jinja2的文档真的很不错。mako的速度比jinja2略快,模板写起来也很舒服。文档略凌乱,能够接受。cheetah的速 度——已经不像是模板了好吧。

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

相关文章
相关标签/搜索