Python 模板引擎性能对比,python性能对比,对比目标,jinja2,


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

方法,随机生成一个二维数组,第一列是自增数据,第二列是长度为100的随机字符串,然后生成html,比较一次生成的时间。说明,如果模板有编译缓存,打开。有其他方法加速,打开。生成缓存,关闭。不计算随机数据生成时间,一次生成后一直使用。以下是文件有效内容,没用的都略去了。最后的顺序是因为我根据结果整理了一下调用次序。

—–testcheetah.tmpl—–    <table>      #for $i in $l      <tr><td>$i[0]</td><td>$i[1]</td>      </tr>      #end for    </table>—–testdjango.html—–    <table>      {% for i in l %}      <tr><td>{{ i.0 }}</td><td>{{ i.1 }}</td>      </tr>      {% endfor %}    </table>—–testjinja2.html—–    <table>      {% for i in l %}      <tr><td>{{ i[0] }}</td><td>{{ i[1] }}</td>      </tr>      {% endfor %}    </table>—–testmako.html—–    <table>      % for i in l:      <tr><td>${i[0]}</td><td>${i[1]}</td>      </tr>      % endfor    </table>—–testwebpy.html—–$def with(l)    <table>      $for i in l:      <tr><td>$i[0]</td><td>$i[1]</td>      </tr>    </table>—–tmpl.py—–#!/usr/bin/python# -*- coding: utf-8 -*-'''@date: 2011-11-03@author: shell.xu'''import os, random, string, timeittestdata = []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 webrender = web.template.render('./')def render_webpy():    return render.testwebpy(testdata)# ——–jinja2——–from jinja2 import Environment, FileSystemLoader, FileSystemBytecodeCacheenv = 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 testcheetahdef render_cheetah():    return testcheetah(searchList = [{'l': testdata},])# ——–mako——–from mako.template import Template as makotmpltmpl_mako = makotmpl(filename = './testmako.html')def render_mako():    return tmpl_mako.render(l = testdata)# ——–django——–from django.template import Template as djangotmplfrom django.template import Contextfrom django.conf import settingssettings.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 SimpleTemplatewith 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_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 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.071762funcname: render_webpy used 0.015729funcname: render_bottle used 0.008752funcname: render_tornado used 0.005675funcname: render_jinja2 used 0.002073funcname: render_mako used 0.001627funcname: 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。看在效率的份上,我可以原谅他大多数的问题。

评论关闭